poj2376 Cleaning Shifts 贪心

poj2376 Cleaning Shifts 贪心

问最少选多少个区间能包含整个区间。

如果一个区间包含另一个区间,那么那个区间就没有作用。
先处理这些区间,使得后一个区间的起始值大于前一个区间的起始值,区间的结束值大于前一个区间的结束值。

再用一个结束值递增的栈,对于每个区间,如果这个区间和栈次顶的区间能相连,能包含栈顶的区间,那么栈顶区间无用,弹出。
最后栈中元素个数就是包含整个区间最小的区间数量。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define NN 25100

struct cow{int s,e;}c[NN];
cow sta[NN];

bool cmp(cow a,cow b){
    if (a.e==b.e) return a.s<b.s;
    else return a.e<b.e;
}

int main(){
    int n,t,p,i,top,fl,ts;
    while(scanf("%d%d",&n,&t)!=EOF){
        for(i=1;i<=n;++i){
            scanf("%d%d",&c[i].s,&c[i].e);
        }
        sort(c+1,c+n+1,cmp);
        p=1;
        for(i=2;i<=n;++i){
            while (p>0&&(c[i].s<c[p].s||(c[i].s==c[p].s&&c[i].e>c[p].e))) --p;
            if (c[i].e!=c[i-1].e) c[++p]=c[i];
        }
        n=p;
        top=0;
        fl=1;
        cow cc;
        cc.s=cc.e=0;
        sta[0]=cc;
        for(i=1;i<=n;++i){
            ts=c[i].s-1;
            while (top>=1&&sta[top-1].e>=ts) top--;
            if (sta[top].e<ts) {fl=0;break;}
            sta[++top]=c[i];
        }
        if (sta[top].e<t) fl=0;
        if (fl)
            printf("%d\n",top);
        else puts("-1");
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值