主要思想是让开始时间<=前一个奶牛的结束时间的所有奶牛中选出结束时间最晚的奶牛
#include <bits/stdc++.h>
using namespace std;
const int N = 25000;
int n, t, res = 0, Max;
struct cow {
int l, r;
bool operator < (cow o) const {
return l < o.l;
}
} a[N];
int main() {
cin >> n >> t;
for(int i = 1; i <= n; i++) {
cin >> a[i].l >> a[i].r;
}
sort(a+1, a+n+1);
for(int i = 1; i <= t; i = Max+1) {
Max = -0x3f3f3f3f;
for(int j = 1; j <= n; j++) {
if(a[j].l <= i)
Max = max(Max, a[j].r);
}
if(Max == -0x3f3f3f3f) {
cout << -1;
return 0;
}
res++;
}
cout << res;
return 0;
}