POJ2376 Cleaning Shifts

题目链接

题目大意:

    N只奶牛做T件事情,第i只能做区间[si,ei]之间的每一件,要求输出做完m件事最少需要的多少头奶牛,若不能完成全部事情就输出-1.(1 <= T <= 1,000,000)(1 <= N <= 25,000)

 

思路

    先按si排序,记录当前做的最后一件事,每次在能与上一只奶牛续上的奶牛中选择结束最晚的;若出现已经续不上或所有奶牛用完也做不完的情况就输出-1。

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 
 5 using namespace std;
 6 
 7 int n,t,ans;
 8 struct node
 9 {
10     int st,ed;
11 }cow[25004];
12 
13 int comp(const node &a,const node &b)
14 {
15     if(a.st<b.st)return true;
16     else return false;
17 }
18 
19 int main()
20 {
21     scanf("%d%d",&n,&t);
22     for(int i=1;i<=n;i++)
23     {
24         scanf("%d%d",&cow[i].st,&cow[i].ed);
25     }
26     sort(cow+1,cow+1+n,comp);
27     int tem=0,i=1;
28     ans=0;
29     while(i<=n)
30     {
31         if(cow[i].st>tem+1)
32         {
33             printf("-1\n");
34             return 0;
35         }
36         int te=tem;
37         while(cow[i].st<=te+1)
38         {
39             tem=max(cow[i].ed,tem);
40             i++;
41         }
42         if(tem!=te)ans++;
43     }
44     if(tem<t)
45     {
46         printf("-1\n");
47         return 0;
48     }
49     printf("%d\n",ans);
50     return 0;
51 }

 

posted @ 2018-10-30 09:58  liqgnonqfu  阅读(114)  评论(0编辑  收藏  举报