第十屆山東省省賽 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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章