poj-2376 Cleaning Shifts (排序+贪心)

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

john有n头牛做打扫工作,他想在t时间内每个时间都至少有一头牛在做打扫工作,第一头牛在1,最后一头牛在t时间,每一头牛工作都有一个开始时间和结束时间,现在让我们找出在每个时间点都有牛打扫的情况下,所用牛越少越好,不能满足输出-1.

首先按起点排序,起点相同就按结束时间长的排序,然后贪心,每次选择时满足      当前牛的开始时间<=上一头牛的结束时间加1,并且当前牛的结束时间最大的一个。

注意 : 不必覆盖只要能连接即可。(1 3     4  6) 是可以的!!!

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <vector>
 5 #include <cstring>
 6 #include <string>
 7 #include <algorithm>
 8 #include <string>
 9 #include <set>
10 #include <functional>
11 #include <numeric>
12 #include <sstream>
13 #include <stack>
14 #include <map>
15 #include <queue>
16 
17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
18 
19 #define ll long long
20 #define inf 0x7f7f7f7f
21 #define lc l,m,rt<<1
22 #define rc m + 1,r,rt<<1|1
23 #define pi acos(-1.0)
24 
25 #define L(x)    (x) << 1
26 #define R(x)    (x) << 1 | 1
27 #define MID(l, r)   (l + r) >> 1
28 #define Min(x, y)   (x) < (y) ? (x) : (y)
29 #define Max(x, y)   (x) < (y) ? (y) : (x)
30 #define E(x)        (1 << (x))
31 #define iabs(x)     (x) < 0 ? -(x) : (x)
32 #define OUT(x)  printf("%I64d
", x)
33 #define lowbit(x)   (x)&(-x)
34 #define Read()  freopen("a.txt", "r", stdin)
35 #define Write() freopen("b.txt", "w", stdout);
36 #define maxn 1000000000
37 #define N 1010
38 using namespace std;
39 
40 struct point
41 {
42     int x,y;
43 }p[25010];
44 bool cmp(const point &a,const point &b)
45 {
46     if(a.x!=b.x) return a.x<b.x;
47     else return a.y>b.y;
48 }
49 int main()
50 {
51    //Read();
52    //Write()
53    int n,t;
54    while(~scanf("%d%d",&n,&t))
55    {
56        for(int i=0;i<n;i++)
57        {
58            scanf("%d%d",&p[i].x,&p[i].y);
59        }
60        sort(p,p+n,cmp);
61        //for(int i=0;i<n;i++)
62        // printf("%d %d
",p[i].x,p[i].y);
63        if(p[0].x!=1) {printf("-1
");continue;}
64        if(p[0].y==t) {printf("1
");continue;}
65        int m=p[0].y,max=p[0].y,ans=1,j=0;
66        bool flag;
67        while(1)
68        {
69            flag=0;
70            for(int i=j+1;i<n;i++)
71            {
72                if(p[i].x<=m+1&&p[i].y>max)
73                {
74                    flag=1;
75                    j=i;
76                    max=p[i].y;
77                }
78            }
79            if(!flag) break;
80            //printf("%d %d
",p[j].x,p[j].y);
81            ans++;
82            if(max==t)
83            {
84                printf("%d
",ans);
85                flag=1;
86                break;
87            }
88            m=max;
89        }
90        if(!flag||max<t) printf("-1
");
91    }
92    return 0;
93 }
View Code
原文地址:https://www.cnblogs.com/nowandforever/p/4408726.html