hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏
huffman coding, greedy algorithm.
std::priority_queue, std::partition,
when i use the three commented lines, excution time increase to 15ms from 0ms, due to worse locality?
thanks to
http://acm.hdu.edu.cn/discuss/problem/post/reply.php?action=support&postid=5699&messageid=2&deep=1
for the provided test case. below is an excerpt,
input
AAAAABCD
THE_CAT_IN_THE_HAT
A
ABCD
AAAAA
ABCDEFGH
END
output
64 13 4.9
144 51 2.8
8 1 8.0
32 8 4.0
40 5 8.0
64 24 2.7
#include <cstdio>
#include <queue>
#include <algorithm>
#include <functional>
int main() {
//freopen("input.txt","r",stdin);
const int MAXSIZE=256;
char line[MAXSIZE], *q;
//int times[MAXSIZE]={0}, i,tmp,len, sum,res, *p,*pu=times+1,*pn=times+(2-'0'),*pc=times+(12-'A');
int times[MAXSIZE]={0}, i,tmp,len, sum,res, *p,*pu=times+1,*pn=times+2,*pc=times+12;
std::priority_queue<int, std::vector<int>, std::greater<int> > my_min_heap;
while(scanf("%256s",line)==1 && strcmp(line,"END")!=0) {
for(q=line;tmp=*q;++q) {
//if(tmp>='A' && tmp<='Z') ++pc[tmp];
//else if(tmp>='0' && tmp<='9') ++pn[tmp];
if(tmp>='A' && tmp<='Z') ++pc[tmp-'A'];
else if(tmp>='0' && tmp<='9') ++pn[tmp-'0'];
else ++*pu;
}
p=std::partition(pu,pu+37,[](int i) { return i>0; });
len=p-pu;
if(len<2) { res=strlen(line); }
else {
while(--p!=times) my_min_heap.push(*p);
for(res=0,i=1;i<len;++i) {
sum=my_min_heap.top(); my_min_heap.pop();
sum+=my_min_heap.top(); my_min_heap.pop();
my_min_heap.push(sum);
res+=sum;
}
my_min_heap.pop();
}
std::fill(pu,pu+len,0);
len=strlen(line)<<3;
printf("%d %d %.1f\n",len,res,(double)len/res);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。// p.s. If in any way improment can be achieved, better performance or whatever, it will be well-appreciated to let me know, thanks in advance.
hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏的更多相关文章
- Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Let the Balloon Rise 分类: HDU 2015-06-19 19:11 7人阅读 评论(0) 收藏
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1036 (I/O routines, fgets, sscanf, %02d, rounding, atoi, strtol) 分类: hdoj 2015-06-16 19:37 32人阅读 评论(0) 收藏
thanks to http://stackoverflow.com/questions/2144459/using-scanf-to-accept-user-input and http://sta ...
- HDU 1754 I Hate It 2016-09-14 19:01 27人阅读 评论(0) 收藏
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏
胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Subm ...
- HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- array_merge注意细节
array_merge:合并一个或多个数组,一个数组中的值加在前一个数组的后面,返回的新数组作为结果 如果输入的数组中有相同的字符串键名,则该键名后面的值覆盖前面的,如果数组包含相同的数字键名,后面的 ...
- 安装appcan后打开eclipse出错
原有eclipse,后安装appcan后打开eclipse出错,因为appcan是自带的编译器也是eclipse所以会产生冲突,只需要在环境变量path里面把java_home参数移到appcan参数 ...
- 纯CSS气泡效果
http://www.liaoxuefeng.com/article/0013738937970388b6b6e5e5e2f4e21b65b01807c84ddf6000
- Python之ftplib模块
一.引言: 某一天,开发哥们跟我反映lftp和java写的ftp程序下载文件有问题,具体情况如下:当一个大于1G的文件已经下载完毕以后一直出现夯住的情况.为了重现开发哥们所说情况,我就自己用pytho ...
- jQuery 选择器 (基础恶补)
jQuery 元素选择器 jQuery 使用 CSS 选择器来选取 HTML 元素. $("p") 选取 <p> 元素. $("p.intro") ...
- Unity-The Courtyard demo学习
1.编辑器相机同步 The Courtyard的demo中,会发现Scene视图和Game视图是编辑器下同步的,它通过一个CopySceneView.cs脚本实现 Scene视图和Game视图的显示效 ...
- 华东交通大学2016年ACM“双基”程序设计竞赛 1008
Problem Description halfyarn找你写个简单的题?好哒!给你n个整数,现在要求你选择两个位置的数,例如选择第pos_a个数a,和第pos_b个数b,给定ans=min(a,b) ...
- easyui datagrid 行右键生成 动态获取(toolbar) 按钮
var createGridRowContextMenu = function(e, rowIndex, rowData) { e.preventDefault(); var grid = $(thi ...
- Visual Studio的Web Performance Test提取规则详解(1)
总结 Visual Studio的Web Performance Test是基于HTTP协议层的,它不依赖于浏览器,通过直接接收,发送HTTP包来和Web服务器交互.Web Performance T ...
- linux内核3.4基于wakeup_source的autosleep机制分析
点击打开链接 一:wakeup_source简介: linux 3.4内核PM使用了wakeup_source来保持唤醒状态,也就是keep awake.之前android一直是基于Linux加入了w ...