2023B-观看文艺汇演(贪心时间安排)

https://oj.algomooc.com/problem.php?id=3110
某公园将举行多场文艺表演,很多演出都是同时进行,一个人只能同时观看一场演出,且不能迟到早退,由于演出分布在不同的演出场地,所以连续观看的演出最少有 15 分钟的时间间隔。
小明是一个狂热的文艺迷,想观看尽可能多的演出。
现给出演出时间表,请帮小明计算他最多能观看几场演出。

输入
第一行为一个数 N,表示演出场数,1 <= N <= 1000。
接下来 N 行,每行两个空格分割的整数,第一个整数 T 表示演出的开始时间,第二个整数 L 表示演出的持续时间,T 和 L 的单位为分钟,0 <= T <= 1440, 0 < L <= 100。

输出
最多能观看的演出场数。

样例输入 复制
2
720 120
840 120
样例输出
1

这个题是经典的贪心,可以直接搜贪心时间安排,这里就不讲了,直接说结论,就是按照结束时间排序,然后再依次找

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e5+100;
struct node{
	int s,e;
	bool operator <(const node &a){
		return e<a.e;
	}
}a[maxn];
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		int c,d;
		cin>>c>>d;
		a[i].s=c;
		a[i].e=c+d+15;
	}
	sort(a+1,a+n+1);
	int ans=1;
	int ee=a[1].e;
	for(int i=2;i<=n;i++){
		if(a[i].s>=ee){
			ans++;
			ee=a[i].e;
		}
	}
	cout<<ans<<endl;
}
posted @ 2024-04-30 21:45  lipu123  阅读(35)  评论(0)    收藏  举报