hdu 1051 (greedy algorithm, how a little modification turn 15ms to 0ms) 分类: hdoj 2015-06-18 12:54 29人阅读 评论(0) 收藏
the 2 version are essentially the same, except version 2 search from the larger end, which reduce the search time in extreme condition from linear to constant, so be faster.
version 1
#include <cstdio>
#include <algorithm>
struct LWPair{
int l,w;
};
int main() {
//freopen("input.txt","r",stdin);
const int MAXSIZE=5000, MAXVAL=10000;
LWPair sticks[MAXSIZE];
int store[MAXSIZE];
int ncase, nstick, length,width, tmp, time, i,j;
if(scanf("%d",&ncase)!=1) return -1;
while(ncase-- && scanf("%d",&nstick)==1) {
for(i=0;i<nstick;++i) scanf("%d%d",&sticks[i].l,&sticks[i].w);
std::sort(sticks,sticks+nstick,[](const LWPair &lhs, const LWPair &rhs) { return lhs.l>rhs.l || lhs.l==rhs.l && lhs.w>rhs.w; });
for(time=-1,i=0;i<nstick;++i) {
tmp=sticks[i].w;
for(j=0;j<=time && store[j]<tmp;++j) ; //search from left to right
if(j>time) { store[++time]=tmp; }
else { store[j]=tmp; }
}
printf("%d\n",time+1);
}
return 0;
}
version 2
#include <cstdio>
#include <algorithm>
struct LWPair{
int l,w;
};
int main() {
//freopen("input.txt","r",stdin);
const int MAXSIZE=5000, MAXVAL=10000;
LWPair sticks[MAXSIZE];
int store[MAXSIZE];
int ncase, nstick, length,width, tmp, time, i,j;
if(scanf("%d",&ncase)!=1) return -1;
while(ncase-- && scanf("%d",&nstick)==1) {
for(i=0;i<nstick;++i) scanf("%d%d",&sticks[i].l,&sticks[i].w);
std::sort(sticks,sticks+nstick,[](const LWPair &lhs, const LWPair &rhs) { return lhs.l>rhs.l || lhs.l==rhs.l && lhs.w>rhs.w; });
for(time=-1,i=0;i<nstick;++i) {
tmp=sticks[i].w;
for(j=time;j>=0 && store[j]>=tmp;--j) ; // search from right to left
if(j==time) { store[++time]=tmp; }
else { store[j+1]=tmp; }
}
printf("%d\n",time+1);
}
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 1051 (greedy algorithm, how a little modification turn 15ms to 0ms) 分类: hdoj 2015-06-18 12:54 29人阅读 评论(0) 收藏的更多相关文章
- hdu 1030 Delta-wave (C++, 0ms, explanatory comments.) 分类: hdoj 2015-06-15 12:21 45人阅读 评论(0) 收藏
problem description http://acm.hdu.edu.cn/showproblem.php?pid=1030 #include <cstdio> #include ...
- hdu 1035 (usage of sentinel, proper utilization of switch and goto to make code neat) 分类: hdoj 2015-06-16 12:33 28人阅读 评论(0) 收藏
as Scott Meyers said in his book Effective STL, "My advice on choosing among the sorting algori ...
- 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 ...
- HDU 2035 人见人爱A^B 分类: ACM 2015-06-22 23:54 9人阅读 评论(0) 收藏
人见人爱A^B Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- Number Sequence 分类: HDU 2015-06-19 20:54 10人阅读 评论(0) 收藏
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- 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 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 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) ...
- 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) ...
随机推荐
- 较全的IT方面帮助文档
http://www.shouce.ren/post/d/id/108632 XSLT参考手册-新.CHMhttp://www.shouce.ren/post/d/id/108633 XSL-FO参考 ...
- 关于mysql中int(1)中int后面的数字
mysql在建表的时候int类型后的长度代表什么? 是该列允许存储值的最大宽度吗? 为什么我设置成int(1), 也一样能存10,100,1000呢. 当时我虽然知道int(1),这个长度1并不代表 ...
- JAVA枚举
枚举类型的使用: 定义一个枚举变量: enum Day{ MOD,TUE,WED,THR,FRI; }; 可以直接输出枚举变量的字符串形式和利用switch输出,在JDK1.6及之前switch只识别 ...
- LeetCode----172. Factorial Trailing Zeroes(Java)
package singlenumber136; //Given an array of integers, every element appears twice except for one. F ...
- libjingle_peerconnection
最近在学习libjingle_peeconnection的代码 不知道应该如何写起,就先从类和各种数据结构列起吧 PeerConnectionFactory: 在创建PeerConnectionFac ...
- noi 4982 踩方格
题目链接:http://noi.openjudge.cn/ch0206/4982/ 深搜很好写. DP:O(n) d[i] 为走 I 不的方案数, l[i],r[i],u[i]为第一步走 左,右,上, ...
- CentOS install GCC-4.8.5
1. 下载源码:http://ftp.gnu.org/gnu/gcc 2. cd gcc-4.8.5 ./contrib/download_prerequisites //下载资源包 3. ...
- php中的匿名函数和闭包(closure)
一:匿名函数 (在php5.3.0 或以上才能使用) php中的匿名函数(Anonymous functions), 也叫闭包函数(closures), 允许指定一个没有名称的函数.最常用的就是回调函 ...
- MySQL安装(转)
本文介绍MySQL的安装 可以单独阅读,也可以作为PHP环境搭建的一部分 PHP完整配置信息请参考 http://www.cnblogs.com/azhe-style/articles/php_env ...
- Trasformation中的公式报错误,错误数据行定位
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...