POJ 2376 Cleaning Shifts【题解】贪心

时间:2019-09-11
本文章向大家介绍POJ 2376 Cleaning Shifts【题解】贪心,主要包括POJ 2376 Cleaning Shifts【题解】贪心使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题面:http://poj.org/problem?id=2376

和luogu的题面有不同,并不是询问最小的代价。

而是询问最小点数。

那么就可以用贪心解决了。

代码如下:

#include<cstdio>
#include<algorithm>
#define int long long
#define ll long long
using namespace std;
int n,ti;
struct node{
    int l,r;
    bool operator < (const node &a) const{
        return l==a.l ? r<a.r : l<a.l;
    }
}c[25010];
signed main()
{
    scanf("%lld%lld",&n,&ti);
    for(int i=1;i<=n;i++)
        scanf("%lld%lld",&c[i].l,&c[i].r);
    sort(c+1,c+1+n);
    c[n+1].l=0x7fffffff;
    bool f=0;
    ll t=0,tp=0,ans=0;
    for(int i=1;i<=n;i++)
        if(c[i].l<=t+1){
            if(c[i].r>tp)
                tp=c[i].r,f=1;
            if(c[i+1].l>t+1 && f) //如果下一个连接不上,那么这一个一定要取。
                t=tp,ans++,f=0;
        }
    if(ti>t) printf("-1\n");
    else printf("%lld\n",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/ChrisKKK/p/11505394.html