zoj 2339 Hyperhuffman 哈夫曼编码 (4-C)
来源:互联网 发布:淘宝双11充值红包退款 编辑:程序博客网 时间:2024/05/17 09:31
题目地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1394
注意要点:
1根本不需要真正构建出树的结构 否则反而超内存
由于最后的结果等于 w【i】*l【i】求和
换成加法 w【i】应该加层数那么多次,而这样的构建方式使根结点的权值恰好等于所有叶子节点权值之和(包括中间状态)
所以在每次创建新结点时,把新结点的权值加到最后的结果中去,那么每个权值被加的次数等于它出于多少颗树中,恰好等于它的高度
2即使是weight 也应该用long long保存 因为p【i】已经可达10^9
#include<iostream>#include<queue>using namespace std;typedef long long inta;struct node{ inta weight; bool operator<(const node & a) const { return weight>a.weight; }};int main(){ int size; cin>>size; for(int l=0;l<size;l++) { priority_queue<node> pq; int n; cin>>n; long long ans=0; for(int i=0;i<n;i++) { int w; cin>>w; node temp; temp.weight=w; pq.push(temp); } while(pq.size()>1) { node n1=pq.top(); pq.pop(); node n2=pq.top(); pq.pop(); node newnode; newnode.weight=n1.weight+n2.weight; pq.push(newnode); ans+=newnode.weight; } cout<<ans<<endl; if(l<size-1) cout<<endl; }}
- zoj 2339 Hyperhuffman 哈夫曼编码 (4-C)
- ZOJ 2339 Hyperhuffman(Huffman编码)
- zoj 2339 Hyperhuffman 赫夫曼编码
- 霍夫曼编码 zoj 2339 Hyperhuffman
- ZOJ 2339 Hyperhuffman
- ZOJ 2339 Hyperhuffman
- sgu203:Hyperhuffman(哈夫曼编码)
- C - Hyperhuffman(哈弗曼值)
- ZOJ2339 Hyperhuffman(霍夫曼编码长度)
- Hyperhuffman (哈夫曼树(水题))
- ZOJ 1117 Entropy(哈夫曼编码)
- ZOJ 3204 (C)
- 哈夫曼编码(C实现)
- sgu-203 Hyperhuffman
- ZOJ2339 Hyperhuffman 优先队列
- c 实现哈夫曼编码
- 哈夫曼编码C语言
- C 实现 哈夫曼编码
- poj2965 The Pilots Brothers' refrigerator
- sdfa
- iOS开发之视图控制器(UIViewController)
- 变幻线
- vs devenv命令用法
- zoj 2339 Hyperhuffman 哈夫曼编码 (4-C)
- NYOJ - 311 《完全背包》 【完全恰满背包】
- PHP版本VC6与VC9、Thread Safe与None-Thread Safe等的区别
- webview js 交互问题
- iOS 模态视图 demo
- zoj 2343 Robbers 尽量按比例分配 (4-G)
- Citrix 服务器虚拟化之三 Xenserver 网络管理
- Spread for ASP.NET 常见安装错误解决办法(Failed while processing WebVirtualDirs)
- zoj 2674 Strange Limit 欧拉定理应用 (3-E)