POJ2376

Cleaning Shifts

AC code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
typedef pair<int, int> ii;
/*
1 5
2 5
3 6
*/
int main()
{
int n, t;
while (scanf("%d %d", &n, &t) == 2) {
vector<ii> inp;
for (int i = 0; i < n; i++) {
int a, b;
scanf("%d %d", &a, &b);
inp.push_back(ii(a, b));
}
sort(inp.begin(), inp.end());
int begin = 1, end = 0, ans = 1;
for (int i = 0; i < n; i++) {
if (inp[i].first <= begin || inp[i].second <= end) {
// the starting point is <= begin or the ending point in less than the
// current ending point
if (inp[i].second > end) // extend the end point as far as you can
end = inp[i].second;
} else {
if (inp[i].first - end <= 1) // no gap between extended intervals
ans++;
else {
ans = -1;
break;
}
begin = end + 1;
i--;
}
}
if (end < t) // crucial
printf("-1\n");
else
printf("%d\n", ans);
}
return 0;
}