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

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

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

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

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

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

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

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

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

随机推荐

  1. jsp通过jQuery返回json数据到页面

    1.首先要导入json的包,自己去网站找,总共6个jar包! 2. 在servlet里的写法(仅共参考) JSONObject json = new JSONObject(); String a = ...

  2. Oracle数据库DOC命令导入导出(2014-3-10记)

    导出:exp lwj/lwj123456@orcl file=d:/db.dmp full=y 导入:imp lwj/lwj123456@orcl file=d:/db.dmp full=y 注:用户 ...

  3. js 操作 字符串

    去w3school看看string的基本方法把: 方法描述 anchor()    创建 HTML 锚.    big()    用大号字体显示字符串.    blink()    显示闪动字符串.  ...

  4. wifi 驱动移植范例

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

  5. 【20160924】GOCVHelper 图像处理部分(3)

    //根据轮廓的圆的特性进行选择     vector<VP> selectShapeCircularity(Mat src,Mat& draw,vector<VP> c ...

  6. python(四)函数

    一.背景 在学习函数之前,一直遵循:面向过程编程,即:根据业务逻辑从上到下实现功能,其往往用一长段代码来实现指定功能,开发过程中最常见的操作就是粘贴复制,也就是将之前实现的代码块复制到现需功能处,如下 ...

  7. EasyUI datebox 只读和取值

    <input id="dd" type="text" class="easyui-datebox" required="re ...

  8. 配置eclipse使能打开当前文件所在目录

    配置方法如下:Run -> External Tools -> External Tools Configurations...右键Program,new 在右边的界面中: Locatio ...

  9. XAF应用开发教程(八) 汉化与多国语言支持

    使用了XAF开发时,汉化是一个比较常的问题. 要实现汉化很简单: 1.在这里下载汉化资源文件.这里演示的版本是15.1.X的 2.文件下载后将:文件解压到目录    <你的项目>\BIN\ ...

  10. Android Activity的加载模式和onActivityResult方法之间的冲突

    前言 今天在调试程序时,发现在某一Activity上点击返回键会调用该Activity的onActivityResult()方法.我一开始用log,后来用断点跟踪调试半天,还是百思不得其解.因为之前其 ...