贪心-区间选点
重点
1. 区间排序
2. start和last用来更新上一个重合区间的左端点和右端点
3. 如果当前的区间的左端点不在重合区间内,上个区间内应该至少放一个点,并同时更新下一个重合区间的端点
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 100010, INF = 0x3f3f3f3f;
typedef pair<int, int> PII;
vector<PII> v;
int n;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
v.push_back({a, b});
}
sort(v.begin(), v.end());
int start = -INF, last = INF, res = 0;
for (int i = 0; i < v.size(); i++) {
//printf("v[%d].first = %d, last = %d\n", i, v[i].first, last);
if (v[i].first > last) {
if (last != INF) res++;
start = v[i].first, last = v[i].second;
} else {
start = v[i].first, last = min(last, v[i].second);
}
}
if (last != INF) res++;
cout << res << endl;
return 0;
}