第十届山东省省赛 H.Tokens on the Segments(贪心)

题意:给你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;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值