• 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
    ",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
    ",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.

  • 相关阅读:
    【转载】10个Web3D可视化精彩案例
    基于react的audio组件
    如何开发一款堪比APP的微信小程序(腾讯内部团队分享)
    CSS3 用border写 空心三角箭头 (两种写法)
    浅谈微信小程序对于创业者,意味着什么?
    左手Cookie“小甜饼”,右手Web Storage
    css3中user-select的用法详解
    个人感觉一些比较有用的特效例子
    纯css模拟电子钟
    蓝桥杯 ALGO-2:最大最小公倍数
  • 原文地址:https://www.cnblogs.com/qeatzy/p/4716224.html
Copyright © 2020-2023  润新知