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. 二叉树JAVA实现

    为了克服对树结构编程的畏惧感和神秘感,下定决心将二叉树的大部分操作实现一遍,并希望能够掌握二叉树编程的一些常用技术和技巧.关于编程实现中的心得和总结,敬请期待!~ [1]  数据结构和表示: 二叉树的 ...

  2. php中rsa加密及解密和签名及验签

    加密的内容长度限制为密钥长度少位,如位的密钥最多加密的内容为个长度. 公钥加密 $public_content=file_get_contents(公钥路径); $public_key=openssl ...

  3. lua 操作中文字符串之截取和长度竖排显示

    前言 在游戏中,我们经常会遇到汉字的多行显示,比如名字竖行显示等.如下图: 为了实现上面的效果,lua实现分行是通过  \n  实现的,所以我们需要取出汉字,然后插入 \n 实现分行效果.还有一种就是 ...

  4. YTU 2346: 中序遍历二叉树

    原文链接:https://www.dreamwings.cn/ytu2346/2606.html 2346: 中序遍历二叉树 时间限制: 1 Sec  内存限制: 128 MB 提交: 12  解决: ...

  5. 【jQuery】window.onload 和 $(document).ready() 的区别

    ... 在Stack Overflow上看到了这个问题,自己翻译了过来. The onload event is a standard event in the DOM, while the read ...

  6. Linux的加密认证功能以及openssl详解

    一.详细介绍加密.解密技术 现在的加密/解密技术主要有三种:对称加密,非对称加密,和单向加密 这三种加密解密技术的组合就是现在电子商务的基础,它们三个有各自最适合的领域,而且所要完成的功能也是不同的, ...

  7. C++string中用于查找的find系列函数浅析

    总述:      以下所讲的所有的string查找函数,都有唯一的返回类型,那就是size_type,即一个无符号整数(按打印出来的算).若查找成功,返回按查找规则找到的第一个字符或子串的位置:若查找 ...

  8. 模拟jquery

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. 【转载】USB2.0接口差分信号线设计

    引 言 通用串行总线(Universal Serial Bus)从诞生发展到今天,USB协议已从1.1过渡到2.0,作为其重要指标的设备传输速度,从1.5 Mbps:的低速和12 Mbps的全速,提高 ...

  10. storm-kafka组件中KafkaOffsetMetric相关统计指标说明

    storm-kafka组件中KafkaOffsetMetric相关统计指标说明 storm-kafka是storm提供的一个读取kakfa的组件,用于从kafka队列中消费数据.KafkaOffset ...