题目链接:区间分组
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 100010;
struct Range
{
int l, r;
bool operator< (const Range &W) const
{
return l < W.l;
}
}range[N];
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i ++)
{
int l, r;
cin >> l >> r;
range[i] = {l, r};
}
sort(range, range + n);
// 用小根堆来维护所有组的右端点的最大值
priority_queue<int, vector<int>, greater<int>> heap;
for(int i = 0; i < n; i ++)
{
auto r = range[i];
// 如果两个区间之间有交集,则为该组创建一个新组
if(heap.empty() || heap.top() >= r.l) heap.push(r.r);
else
{
heap.pop();
heap.push(r.r);
}
}
cout << heap.size() << endl;
return 0;
}