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) ...
随机推荐
- 关于学习Knockoutjs--入门(二)
这两天终于闲一丢丢了,可以有多点时间学习一下拉.接下来要写到的还是Knockoutjs. Knockout是建立在以下3个核心功能之上的: 1. 属性监控与依赖跟踪 2. 声明式绑定 3. 模版机制 ...
- Spring 文章推荐
spring mvc 异常统一处理方式:http://www.cnblogs.com/xd502djj/archive/2012/09/24/2700490.html 在springmvc中使用hib ...
- 原生JS--Ajax
原生Ajax: Ajax基础:--ajax:无刷新数据读取,读取服务器上的信息--HTTP请求方法: --GET:用于获取数据,如浏览帖子 --POST:用于上传数据,如用户注册 --GE ...
- Flowplayer-Subtitle
SOURCE URL: https://flowplayer.org/docs/subtitles.html Setting up Subtitles are loaded with a <tr ...
- c#委托(1)
// 委托声明 -- 定义一个签名:delegate double MathAction(double num); class DelegateTest{ // 符合委托声明的常规方法 static ...
- java内部类以及匿名类
内部类 一个类内部定义的类称为内部类. 内部类允许把逻辑相关的类组织在一起,并控制内部代码的可视性. 内部类与外部类的结构层次如下. 顶层类:最外层的类 外部类:内部类所在的类 内部类:类内部定义的类 ...
- php : 匿名函数(闭包) [二]
摘自: http://www.cnblogs.com/yjf512/archive/2012/10/29/2744702.html php的闭包(Closure)也就是匿名函数.是PHP5.3引入的. ...
- iOS - OC NSStream 文件流
前言 @interface NSStream : NSObject @interface NSOutputStream : NSStream 1.文件流的使用 NSString *filePath = ...
- Ubuntu 16.04 更新源
1/ 在修改source.list前,最好先备份一份 执行备份命令 sudo cp /etc/apt/sources.list /etc/apt/sources.list.old 2/ 执行命令打开s ...
- ring0
Intel的x86处理器是通过Ring级别来进行访问控制的,级别共分4层,RING0,RING1,RING2,RING3.Windows只使用其中的两个级别RING0和RING3. RING0层拥有最 ...