ACdream Andrew Stankevich's Contest (2) 哈夫曼树
来源:互联网 发布:阿尔及利亚地图软件 编辑:程序博客网 时间:2024/05/16 00:50
题目链接:
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
可以从其中发现规律,就是要求sum(wi*li),,,可以把乘法看成加法,wi*li相当于加上li层个wi,然后从底向上追溯,会发现最后结果就是所有子节点的权值之和(wi就是权值,li是根节点到子节点的距离)用优先队列处理即可
#include <iostream>#include <stdio.h>#include <string.h>#include <string>#include <cstdio>#include <cmath>#include <queue>typedef long long ll;using namespace std;struct node{ ll weight; bool operator<(const node & a) const { return weight>a.weight; }};int main(){ int n; while(scanf("%d",&n)!=EOF) { priority_queue<node>p; ll sum=0; for(int i=1;i<=n;i++) { ll temp; scanf("%lld",&temp); node q; q.weight=temp; p.push(q); } while(p.size()>1) { node t1=p.top(); p.pop(); node t2=p.top(); p.pop(); node q; q.weight=t1.weight+t2.weight; p.push(q); sum=sum+t1.weight+t2.weight; } printf("%lld\n",sum); } return 0;}
- ACdream Andrew Stankevich's Contest (2) 哈夫曼树
- ACdream Andrew Stankevich's Contest(1)
- ACdream Andrew Stankevich's Contest(1)
- 【ACdream】Andrew Stankevich's Contest (22)
- 【ACdream】Andrew Stankevich's Contest (3)
- 【ACDream】Andrew Stankevich's Contest (23)
- 【ACdream】Andrew Stankevich's Contest (4)
- Andrew Stankevich's Contest #2 Solution
- 【ACdream】1223 Roads Andrew Stankevich Contest 2 KM匹配
- Andrew Stankevich's Contest (1)
- Andrew Stankevich's Contest (21)
- Andrew Stankevich's Contest 23
- 【ACdream】Andrew Stankevich Contest (1)
- ZOJ 2313 Andrew Stankevich's Contest #1
- Andrew Stankevich's Contest #1 Solution
- Andrew Stankevich's Contest #8 Solution
- Andrew Stankevich's Contest #5 Solution
- Andrew Stankevich's Contest #10 Solution
- PAT 2-13 两个有序序列的中位数(C语言实现)
- Zoj 3535 Gao the String II (AC自动机+dp)
- MapReduce 编程 系列十 使用HashPartitioner来调节Reducer的计算负载
- 复分析中的欧拉恒等式
- 【人脸识别】人脸识别必读论文
- ACdream Andrew Stankevich's Contest (2) 哈夫曼树
- 三分钟学会刻录光盘
- 在odl中如何实现rpc
- Selenium Webdriver java 积累一
- Weka算法Clusterers-Xmeans源码分析(二)
- HTML DOM backgroundPositionY 属性
- c++运算符重载以及一些基本概念
- MIT的C指针练习代码
- 指针 引用 对比