forked from yogykwan/acm-challenge-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoj2376.cpp
48 lines (44 loc) · 1.03 KB
/
poj2376.cpp
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
/*
* POJ 2376: Cleaning Shifts
* 题意:有1~T连续的T份任务,有N头奶牛,每头牛可以完成某段连续时间的任务。求最少需要多少头牛,完成T个任务。
* 类型:贪心
* 算法:将N个区间按起点排序,若1~p已全覆盖,则从剩下的待选区间内找到所有起点不大于p,且终点最大的区间。
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
pair <int, int> cows[25010];
int main() {
int T, N;
scanf("%d%d", &N, &T);
for(int i = 0; i < N; ++i) {
scanf("%d%d", &cows[i].first, &cows[i].second);
}
sort(cows, cows + N);
int p = 1;
int cnt = 0;
int i = 0;
while(p <= T) {
int np = -1;
for(;i < N; ++i) {
if(cows[i].first <= p) {
np = max(np, cows[i].second);
} else {
break;
}
}
if(np == -1) {
printf("-1\n");
return 0;
} else {
p = np + 1;
++cnt;
if(p > T) {
printf("%d\n", cnt);
return 0;
}
}
}
return 0;
}