ZOJ 2339 Hyperhuffman(Huffman编码)

来源:互联网 发布:免费qq群排名优化软件 编辑:程序博客网 时间:2024/05/21 06:32

Hyperhuffman

You might have heard about Huffman encoding - that is the coding system that minimizes the expected length of the text if the codes for characters are required to consist of an integral number of bits.

Let us recall codes assignment process in Huffman encoding. First the Huffman tree is constructed. Let the alphabet consist of N characters, i-th of which occurs Pi times in the input text. Initially all characters are considered to be active nodes of the future tree, i-th being marked with Pi. On each step take two active nodes with smallest marks, create the new node, mark it with the sum of the considered nodes and make them the children of the new node. Then remove the two nodes that now have parent from the set of active nodes and make the new node active. This process is repeated until only one active node exists, it is made the root of the tree.

Note that the characters of the alphabet are represented by the leaves of the tree. For each leaf node the length of its code in the Huffman encoding is the length of the path from the root to the node. The code itself can be constrcuted the following way: for each internal node consider two edges from it to its children. Assign 0 to one of them and 1 to another. The code of the character is then the sequence of 0s and 1s passed on the way from the root to the leaf node representing this character.

In this problem you are asked to detect the length of the text after it being encoded with Huffman method. Since the length of the code for the character depends only on the number of occurences of this character, the text itself is not given - only the number of occurences of each character. Characters are given from most rare to most frequent.

Note that the alphabet used for the text is quite huge - it may contain up to 500 000 characters.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Input

The first line of the input file contains N - the number of different characters used in the text (2 <= N <= 500 000). The second line contains N integer numbers Pi - the number of occurences of each character (1 <= Pi <= 109, Pi <= Pi+1 for all valid i).

Output

Output the length of the text after encoding it using Huffman method, in bits.

Sample Input

1

3
1 1 4

Sample Output

8

ps:裸的Huffman编码,2种方法
1.先将所有数据放入堆。然后每次弹出最小的2个,将它们的和计算到结果中(树多一层编码长度也就加上该结点的值),并将和加入堆。重复n-1次,即可求得最后结果。复杂度O(NlogN)

代码:

#include<stdio.h>#include<queue>using namespace std;typedef long long LL;int main(){    int t,n;    scanf("%d",&t);    while(t--)    {        priority_queue<LL,vector<LL>,greater<LL> >q;        scanf("%d",&n);        LL ans=0,x,y;        for(int i=1; i<=n; ++i)        {            scanf("%lld",&x);            q.push(x);        }        while(!q.empty())        {            x=q.top();            q.pop();            if(q.empty())                break;            y=q.top();            q.pop();            ans+=x+y;            q.push(x+y);        }        printf("%lld\n",ans);        if(t)            puts("");    }    return 0;}

2.用两个递增队列,输入数据已经保证是递增的(A队列),另一个队列(B队列)为空。每次取两个队首中较小的2个值,和1的做法一样,只是和要加入到B队列的队尾。可以证明加入的值保证B仍然是递增的。复杂度O(N)。

代码:

#include<stdio.h>#define maxn 500010typedef long long LL;const LL inf=100000000000;LL a[maxn],b[maxn];int head1,tail1,head2,tail2;LL getmin(){    LL x=inf,y=inf;    if(head1<tail1)        x=a[head1];    if(head2<tail2)        y=b[head2];    if(x<y)    {        ++head1;        return x;    }    else    {        ++head2;        return y;    }}int main(){    int t,n;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(int i=1; i<=n; ++i)            scanf("%d",&a[i]);        head1=1,tail1=n+1,head2=1,tail2=1;        LL ans=0,x,y;        for(int i=1; i<=n-1; ++i)        {            x=getmin();            y=getmin();            ans+=x+y;            b[tail2++]=x+y;        }        printf("%lld\n",ans);        if(t)            puts("");    }    return 0;}
1 0