先对所有区间按左端点大小排序,然后分三种情况讨论来操作
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<pair<int, int>> a;
inline bool cmp(pair<int, int>& x, pair<int, int>& y) {
return x.first > y.first ? false : true;
}
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; ++i) {
int l, r;
cin >> l >> r;
a.push_back({l, r});
}
int res = 1;
sort(a.begin(), a.end(), cmp);
//for(int i = 0; i < n; ++i)
//cout << a[i].first << endl;
pair<int, int> cur = a[0];
for(int i = 1; i < n; ++i) {
if(cur.second >= a[i].second) continue;
else if(a[i].first <= cur.second) cur.second = a[i].second;
else {
++res;
cur = a[i];
}
}
cout << res;
return 0;
}