C - Hyperhuffman(哈弗曼值)
来源:互联网 发布:笔顺查询软件下载 编辑:程序博客网 时间:2024/05/17 08:40
C - Hyperhuffman
Problem Description
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.
Input
Output
Sample Input
31 1 4
Sample Output
8
#include<stdio.h>#include<iostream>#include<queue>using namespace std;#define ll long longtypedef struct nnn{ ll sum,v; friend bool operator<(struct nnn a,struct nnn b) { return a.v>b.v; }}node;priority_queue<node>q;ll count(){ node p1,p2; while(!q.empty()) { p1=q.top(); q.pop(); if(q.empty()) return p1.sum; p2=q.top(); q.pop(); p1.sum+=p2.sum+p1.v+p2.v; p1.v+=p2.v; q.push(p1); }}int main(){ node p; int n; while(scanf("%d",&n)>0) { while(!q.empty())q.pop(); while(n--) { scanf("%lld",&p.v); p.sum=0; q.push(p); } printf("%lld\n",count()); }}
- C - Hyperhuffman(哈弗曼值)
- zoj 2339 Hyperhuffman 哈夫曼编码 (4-C)
- Hyperhuffman (哈夫曼树(水题))
- ZOJ 2339 Hyperhuffman(Huffman编码)
- ZOJ 2339 Hyperhuffman
- sgu-203 Hyperhuffman
- sgu203:Hyperhuffman(哈夫曼编码)
- ZOJ 2339 Hyperhuffman
- ZOJ2339 Hyperhuffman 优先队列
- zoj 2339 Hyperhuffman 赫夫曼编码
- ZOJ2339 Hyperhuffman(霍夫曼编码长度)
- 霍夫曼编码 zoj 2339 Hyperhuffman
- 内存整理(C/C++)
- (C/C++)
- 内存整理(C/C++)
- 密码学(C/C++)
- C&C++)试题
- utilities(C/C++)
- 内存的堆分配和栈分配 & 字符数组,字符指针,Sizeof总结
- C语言四书五经
- android https 连接建立
- POJ2553——The Bottom of a Graph
- 如何从android官网下载开发工具
- C - Hyperhuffman(哈弗曼值)
- 避免Android开发中的ANR
- Myeclipse快捷操作
- 【Ardunio】开发入门教程【二】数据类型
- 人生感悟:人生像吃自助餐
- 怎样花两年时间去面试一个人
- 开发入门教程【三】Arduino开发工具
- 高效开发Android App的10个建议
- 【Arduino】开发入门【四】Arduino驱动安装