AcWing 803. 区间合并
原题链接
简单
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
typedef pair<int, int> PII;
vector<PII> p;
int main()
{
int m;
cin >> m;
ios::sync_with_stdio(false);
cin.tie(0);
if(m == 0) {cout << 0; return 0;}
for(int i = 0; i < m; i ++ )
{
int l, r;
cin >> l >> r;
p.push_back({l,r});
}
sort(p.begin(), p.end());
int res = 1, l = p[0].first, r = p[0].second;
for(int i = 1; i < m; i ++ )
{
if(p[i].first > r)
{
res ++ ;
l = p[i].first;
r = p[i].second;
}
else if(p[i].second > r) r = p[i].second;
}
cout << res;
return 0;
}