POJ 2376 Cleaning Shifts

来源:互联网 发布:雾霾吸入 知乎 编辑:程序博客网 时间:2024/06/06 20:00

  • Source Code
    #include<iostream>#include<algorithm>using namespace std;struct node{int x, y;}cow[25002];bool cmp(node a, node b){return a.x < b.x;}int main(){int n, k;while (cin >> n >> k){bool xx = 0, yy = 0;for (int i = 0; i < n; i++){cin >> cow[i].x >> cow[i].y;if (cow[i].x == 1) xx = 1;if (cow[i].y == k) yy = 1;}if (xx != 1 || yy != 1){cout << "-1" << endl; continue;}sort(cow, cow + n, cmp); cow[n].x = 1000003;int start = 0, temp = 0; bool b = 0; int s = 0;for (int i = 0; i < n; i++){if (cow[i].x <= start+1){if (cow[i].y > temp) temp = cow[i].y, b = 1;if (cow[i + 1].x > start+1&&b){start = temp; s++; b = 0;}}}if (start >= k) cout << s << endl;else cout << "-1" << endl;}return 0;}

原创粉丝点击