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) 收藏的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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) ...

  8. 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 ...

  9. 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) ...

随机推荐

  1. html5,output标签应用举例

    <form action="" id="myform" oninput="num.value=parseInt(num1.value)+pars ...

  2. foreach遍历遇到的一个细节问题

    1.Invalid argument supplied for foreach()警告错误解决办法:foreach遍历之前添加is_array()判断

  3. 卸载cygwin

    1.下载takeown.exe(貌似win自带,你可以打开cmd 输入takeown.exe查看,貌似不行)所以还是去网上下载吧,亲测http://download.csdn.net/download ...

  4. wifi 驱动移植范例

    .改Makefile:  里面没有dm6441平台的,我看到有dm6446的,所以就在这里改了 ifeq ($(PLATFORM),DM6446) LINUX_SRC = /root/work/lin ...

  5. [课程设计]Sprint Two 回顾与总结&发表评论&团队贡献分

    [课程设计]Sprint Two 回顾与总结&发表评论&团队贡献分 ● 一.回顾与总结 (1)回顾 燃尽图: Sprint计划-流程图: milestones完成情况如下: (2)总结 ...

  6. angularJS ng-grid 配置

    以下是按我的需求修改的 简单的demo  可以自己扩展 HTML: <!DOCTYPE html> <html class="no-js" ng-app=&quo ...

  7. YTU 3025: 创建二叉树

    原文链接:https://www.dreamwings.cn/ytu3025/2628.html 3025: 创建二叉树 时间限制: 1 Sec  内存限制: 128 MB 提交: 3  解决: 3 ...

  8. fork &vfork --陈皓

    http://coolshell.cn/articles/7965.html http://coolshell.cn/articles/12103.html#more-12103 前两天有人问了个关于 ...

  9. C++头文件的组织

    转自:http://www.cnblogs.com/lidabo/archive/2012/04/17/2454568.html C++编译模式通常,在一个C++程序中,只包含两类文件——.cpp文件 ...

  10. GMF中,删除节点和连线的另一种实现

    问题 在GMF中,如果需要programmatically删除节点或连线,在google中我们很容易搜索到<GMF中,删除节点和连线的实现>一文(我并不确定这是原创作者的原始链接),很多人 ...