Files
python/TangDou/AcWing/Section/905_PII.cpp

32 lines
866 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
#define l second
#define r first
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
int n;
PII a[N];
/*
由于pair会自动以first升序排列所以用pair就不用自己重载小于运算符,至于first和second的先后关系其实没什么意义。
只要在输入/存储时交换一下x,y顺序即可,如果觉得容易混可以直接定义l为second,r为first就不会混了,同理以左端点排序的题一样
*/
int res, ed = -INF;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
a[i] = {y, x};
}
sort(a, a + n);
for (int i = 0; i < n; i++) {
if (a[i].l > ed) {
res++;
ed = a[i].r;
}
}
cout << res << endl;
return 0;
}