• bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 -- 贪心


    3389: [Usaco2004 Dec]Cleaning Shifts安排值班

    Time Limit: 1 Sec  Memory Limit: 128 MB

    Description

        一天有T(1≤T≤10^6)个时段.约翰正打算安排他的N(1≤N≤25000)只奶牛来值班,打扫
    打扫牛棚卫生.每只奶牛都有自己的空闲时间段[Si,Ei](1≤Si≤Ei≤T),只能把空闲的奶牛安排出来值班.而且,每个时间段必需有奶牛在值班.  那么,最少需要动用多少奶牛参与值班呢?如果没有办法安排出合理的方案,就输出-1.

    Input

     
        第1行:N,T.
        第2到N+1行:Si,Ei.

    Output

     
        最少安排的奶牛数.

    Sample Input


    3 10
    1 7
    3 6
    6 10

    Sample Output


    2


    样例说明
    奶牛1和奶牛3参与值班即可.

    HINT

    排序后贪心

    #include<map>
    #include<cmath>
    #include<queue>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define N 25010
    struct qaz{int l,r;}a[N];
    bool cmp(qaz a, qaz b){return a.l==b.l?a.r<b.r:a.l<b.l;}
    int n,T,now,cnt,tmp,ans;
    int main()
    {
        scanf("%d%d",&n,&T);
        for(int i=1;i<=n;i++) scanf("%d%d",&a[i].l,&a[i].r);
        sort(a+1,a+n+1,cmp);
        while(now<T)
        {
            tmp=now;
            while(cnt<n&&a[cnt+1].l<=tmp+1){cnt++;now=max(now,a[cnt].r);}
            if((cnt==n&&a[cnt].r<T)||(a[cnt+1].l>now+1)){puts("-1");return 0;}
            ans++;
        }
        now<T?puts("-1"):printf("%d
    ",ans);
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    input file 上传图片并显示
    关于npm ---- npm 命令行运行多个命令
    webpack4.x 配置
    React的生命周期
    HTML5 meta 属性整理
    css 命名规范
    html5 标签 meter 和 progress
    .NET Linq TO XML 操作XML
    .NET 字符串指定规则添加换行
    Linux Centos上部署ASP.NET网站
  • 原文地址:https://www.cnblogs.com/lkhll/p/6790369.html
Copyright © 2020-2023  润新知