poj2376 Cleaning Shifts(区间贪心,理解题意)

https://vjudge.net/problem/POJ-2376

题意理解错了!!真是要仔细看题啊!!

看了poj的discuss才发现,如果前一头牛截止到3,那么下一头牛可以从4开始!!!

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<set>
 8 #define INF 0x3f3f3f3f
 9 typedef long long ll;
10 using namespace std;
11 typedef struct{
12     int x, y;
13 }Node;
14 Node node[100010];
15 bool cmp(const Node a, const Node b)
16 {
17     if(a.x != b.x) 
18         return a.x<b.x;
19     else 
20         return a.y>b.y;
21 }//起点相同则返回终点大的,否则返回起点小的 
22 int main()
23 {
24     int n, t, cnt=1, flag=0;
25     cin >> n >> t;
26     for(int i = 0; i < n; i++){
27         cin >> node[i].x >> node[i].y;
28     }
29     sort(node, node+n, cmp);
30     if(node[0].x != 1){
31         cout << "-1" << endl;
32         return 0;
33     }
34     else if(node[0].y == t){
35         cout << "1" << endl;
36         return 0;
37     }
38     else {
39         int end = node[0].y, pre, maxm = node[0].y;
40         for(int i = 1; i < n; i++){
41             pre = maxm;
42             while(i < n&&node[i].x<=end+1){//这里理解错了!! 
43                 if(maxm < node[i].y){
44                     maxm = node[i].y;
45                 }
46                 i++;
47             }
48             i--;
49             //cout << maxm << endl;
50             if(maxm == t){
51                 cnt++;
52                 flag=1;
53                 break;
54             }
55             else if(pre == maxm){
56                 break;
57             }
58             else{
59                 end = maxm;
60                 cnt++;
61             }
62         }
63         if(!flag) cout << "-1" << endl;
64         else
65             cout << cnt << endl;
66     }
67     return 0;
68 }
posted @ 2018-05-06 11:01  Surprisez  阅读(151)  评论(0编辑  收藏  举报