NOI2015 Homer

Topic links: poke me

First look out this is a Huffman tree!

Then follow there Huffman tree that point to say , out of this problem can be friends A

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define MAXN 100010
using namespace std;
int n,k;
long long ans;
long long a[MAXN];
struct Node
{
    long long h,w;
    friend bool operator < (struct Node x,struct Node y)
    {
        if(x.w==y.w) return x.h>y.h;
        return x.w>y.w;
    }
};
priority_queue<Node>q;
inline void solve()
{
    while(q.size()>1)
    {
        long long cur_ans=0,maxx=0;
        for(int i=1;i<=k;i++)
        {
            cur_ans+=q.top().w;
            maxx=max(maxx,q.top().h);
            q.pop();
        }
        ans+=cur_ans;
        // printf("cur_ans=%lld maxx=%lld\n",cur_ans,maxx);
        q.push((Node){maxx+1,cur_ans});
        // cout<<q.size()<<endl;
    }
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("ce.in","r",stdin);
    #endif
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]),q.push((Node){0,a[i]});
    int cur=1;
    while(cur<n) cur+=(k-1);
    for(int i=1;i<=cur-n;i++) q.push((Node){0,0});
    // printf("cur=%d\n",cur);
    solve();
    printf("%lld\n",ans);
    printf("%lld\n",q.top().h);
    return 0;
}

Guess you like

Origin www.cnblogs.com/fengxunling/p/10947454.html