分析:先将区间按左端点排序,然后建一个小堆存放以有组的右端点,然后遍历区间,当堆为空或堆中最小的右端点都大于当前区间的左端点,
则当前区间另分一组,否则就将当前区间划分给堆中最小左端点的那个区间,并更新这个区间的右端点,最后返回堆的大小
#include <bits/stdc++.h> using namespace std; const int N = 100010; int n; struct Edge{int a, b;} edges[N]; bool operator <(Edge e1, Edge e2) { return e1.a < e2.a; } int main() { scanf("%d",&n); for(int i = 0; i < n; i++) { int a, b; scanf("%d%d",&a,&b); edges[i] = {a,b}; } sort(edges,edges+n); priority_queue<int,vector<int>,greater<int>> q; for(int i = 0; i < n; i++) { int a = edges[i].a, b = edges[i].b; if(q.size() == 0 || q.top() >= a) { q.push(b); } else { q.pop(); q.push(b); } } printf("%d ",q.size()); return 0; }