思路:
关于区间的贪心。注意特判。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 pair<int, int> p[25005]; 7 int n, t; 8 9 int main() 10 { 11 cin >> n >> t; 12 for (int i = 0; i < n; i++) 13 { 14 scanf("%d %d", &p[i].first, &p[i].second); 15 } 16 sort(p, p + n); 17 bool flag = true; 18 int now_end = 0; 19 int cnt = 0; 20 for (int i = 0; i < n; i++) 21 { 22 if (p[i].second <= now_end) 23 continue; 24 if (p[i].first - now_end > 1) 25 { 26 flag = false; 27 break; 28 } 29 int j = i, maxn = p[j].second; 30 while (j < n && (p[j].first <= now_end || p[j].first - now_end <= 1)) 31 { 32 maxn = max(maxn, p[j].second); 33 j++; 34 } 35 now_end = maxn; 36 cnt++; 37 if (now_end >= t) 38 break; 39 i = j - 1; 40 } 41 if (now_end < t) 42 { 43 flag = false; 44 } 45 cout << (flag ? cnt : -1) << endl; 46 return 0; 47 }