You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int n; // 线段数量
int res; // 结果
int ed = -INF; // 当前覆盖区间的结束边界,即右端点位置
// 结构体
struct Node {
int l, r;
// 按每个区间的右端点从小到大排序
const bool operator<(const Node &b) const {
return r < b.r;
}
} range[N];
int main() {
cin >> n;
// 注意这里的数组下标是从0开始的
for (int i = 0; i < n; i++) cin >> range[i].l >> range[i].r;
// 右端点从小到大排序排序也需要从数组下标1开始
sort(range, range + n);
// 思想:① 所有区间按右端点从小到大排序
// ② 遍历每一个区间,如果当前区间的左与前一个区间的右有交集,则只需要一个点就可以覆盖掉两个区间
for (int i = 0; i < n; i++)
if (range[i].l > ed) {
res++;
ed = range[i].r;
}
cout << res << endl;
return 0;
}