POJ 3253

Huffman树 详见http://blog.csdn.net/shuangde800/article/details/7341289

然而我用小根堆过的
跟合并果子一样


#include
#include
#include
#include
using namespace std;

long long i,j,k,m,n,a[20001],ans;

inline bool cmp(long long a,long long b){return a>b;}

inline long long readint()
{
    char c=getchar();long long x=0;
    while(c<'0'||c>'9')c=getchar();
    while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
    return x;
}

int main()
{
    n=readint();
    for(i=1;i<=n;i++)a[i]=readint();
    make_heap(a+1,a+n+1,cmp);
    while(n>1)
    {
        pop_heap(a+1,a+n+1,cmp);
        pop_heap(a+1,a+n,cmp);
        a[n-1]+=a[n];
        ans+=a[n-1];n--;
        push_heap(a+1,a+n+1,cmp);
    }
    cout<return 0;
}

你可能感兴趣的:(树)