zoj 2339 Hyperhuffman 赫夫曼编码


很典型的huffman编码,之前写的时候把树给构造出来了,可能会爆内存,注意一点,还是可以不爆。


其实不用构造huffman树,给你n个元素,经过n-1次操作,会生成n-1个新元素。


这样计算带权路径长度


初始化ans=0;


对于每一次新生成元素的权值,都加到ans里面即可,这样相当于 每个子结点都计算了它的高度(最顶层为第0层)次。



Hyperhuffman

Time Limit: 5 Seconds       Memory Limit: 32768 KB

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


#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn=500000    ;
ll ans;


int n;


struct Node
{
    ll key;
    int ind;
    Node(){}
    Node(int ind,ll key):ind(ind),key(key){}

} a[2*maxn+20];

queue<int>q[2];

int cnt;
int get()
{
    if(q[0].empty())
    {
        int ans=q[1].front();
        q[1].pop();
        return ans;
    }
     if(q[1].empty())
    {
        int ans=q[0].front();
        q[0].pop();
        return ans;
    }
   int x=q[0].front();
    int y=q[1].front();
    if(a[x].key<a[y].key) {q[0].pop();return x;}
    else {q[1].pop();return y; }




}



void work()
{
    for(int k=0;k<2;k++)
    {
        while(!q[k].empty())  q[k].pop();
    }

    for(int i=1;i<=n;i++)
    {
        q[0].push(i);
    }
    for(int i=1;i<n;i++)
    {

        int x=get();
        int y=get();
        ll key1=a[x].key;
        ll key2=a[y].key;

        cnt++;
        a[cnt]=Node(cnt,key1+key2 );
        q[1].push(cnt);
        ans+=key1+key2;
    }

}


int main()
{
    int T,kase=0;scanf("%d",&T);
    while(T--)
    {


        if(kase++)  putchar('\n');
        scanf("%d",&n);
        cnt=n;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i].key);
            a[i].ind=i;

        }

        ans=0;
        work();
        printf("%lld\n",ans);
    }

   return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值