简单贪心,trick蛮多的,调了许久~~~
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int MAXN = 25010; const int INF = 0x3f3f3f3f; struct Node { int l, r; Node() {} Node(int t_l, int t_r) : l(t_l), r(t_r) {} friend bool operator < (const Node &p1, const Node &p2) { if(p1.l == p2.l) return p1.r > p2.r; return p1.l < p2.l; } }p[MAXN]; int N, T, total, ans; int main() { //freopen("aa.in", "r", stdin); //freopen("bb.out", "w", stdout); while(scanf("%d %d", &N, &T) != EOF) { for(int i = 0; i < N; ++i) { scanf("%d %d", &p[i].l, &p[i].r); } sort(p, p + N); if(p[0].l > 1) { printf("-1\n"); } else { int id = 0; total = 0; ans = 0; bool flag = true; while(id < N) { total++; int t_max = -INF; flag = false; while(p[id].l <= total && id < N) { t_max = max(t_max, p[id].r); id++; flag = true; } if(!flag) break; total = t_max, ans++; if(total >= T) break; } if(!flag || total < T) printf("-1\n"); else printf("%d\n", ans); } } return 0; }