思路: 排序+贪心, 按左端点进行排序,会有三种情况
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1e4 + 10;
typedef pair<int, int> PII;
vector<PII> segs;
int n;
int main()
{
cin >> n;
for(int i = 0; i < n; i ++ )
{
int l, r;
cin >> l >> r;
segs.push_back({l, r});
}
sort(segs.begin(), segs.end());
int st = -2e9, ed = -2e9;
int res = 0;
for(auto seg : segs)
{
if(ed < seg.first)
{
st = seg.first, ed = seg.second;
res ++;
}
else if(ed < seg.second) ed = seg.second;
}
cout << res << endl;
return 0;
}