题意:给你n条线段的起始和结束位置,每一条线段都可以被它覆盖的一个点标记,一个点只能标记一个线段,求最多可以标记多少条线段?
题解:直接贪心,按照左端点从小到大排序,若相同则按照右端点从小到大排序,然后从第一个线段的左端点开始考虑,若标记成功,则记录当前标记到了哪一个点,若当前线段左端点与之前标记的点相同,则将左端点+1再次放入优先队列中......
#include<bits/stdc++.h>
using namespace std;
#define Sheryang main
const int maxn=2e5+7;
typedef long long ll;
const int mod=1e9+7;
//#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], *p1 = buf, *p2 = buf;
#define IO cin.tie(0),ios::sync_with_stdio(false);
#define pi acos(-1)
#define PII pair<ll,ll>
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();if(c == '-')Nig = -1,c = getchar();while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();return Nig*x;}
#define read read()
/** keep hungry and keep calm! **/
struct node{
int st,ed;
friend bool operator <(node a,node b){
if(a.st!=b.st){
return a.st > b.st;
}
return a.ed > b.ed;
}
};
int Sheryang(){
int T = read;
while(T--){
int n = read;
priority_queue<node>q;
for(int i=1;i<=n;i++){
int st = read , ed = read;
q.push(node{st,ed});
}
int now = 0 , ans = 0;
while(!q.empty()){
node t = q.top();q.pop();
if(t.st<=now&&t.st+1<=t.ed){
q.push(node{t.st+1,t.ed});
continue;
}
if(t.st>now){
ans ++;
now = t.st;
}
}
printf("%d\n",ans);
}
return 0;
}