TZOJ 4007 The Siruseri Sports Stadium(区间贪心)

本文介绍了一个体育场比赛调度问题,通过贪心算法实现最优赛事安排,确保最多数量的比赛能在不冲突的情况下进行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述

The bustling town of Siruseri has just one sports stadium. There are a number of schools, colleges, sports associations, etc. that use this stadium as the venue for their sports events.

Anyone interested in using the stadium has to apply to the Manager of the stadium indicating both the starting date (a positive integer S) and the length of the sporting event in days (a positive integer D) they plan to organise. Since these requests could overlap it may not be possible to satisfy everyone.

It is the job of the Manager to decide who gets to use the stadium and who does not. The Manager, being a genial man, would like to keep as many organisations happy as possible and hence would like to allocate the stadium so that maximum number of events are held.

Suppose, for example, the Manager receives the following 4 requests:

Event No.   Starting Date       Length
           1                   2                    5
           2                   9                    7
           3                  15                   6
           4                   9                    3
He would allot the stadium to events 1, 4 and 3. Event 1 begins on day 2 and ends on day 6, event 4 begins on day 9 and ends on day 11 and event 3 begins on day 15 and ends on day 20. You can verify that it is not possible to schedule all the 4 events (since events 2 and 3 overlap and only one of them can get to use the stadium).

Your task is to help the manager find the best possible allotment (i.e., the maximum number of events that can use the stadium).

输入

The first line of the input will contain a single integer N (N ≤ 100000) indicating the number of events for which the Manager has received a request. Lines 2,3,...,N+1 describe the requirements of the N events. Line i+1 contains two integer Si and Di indicating the starting date and the duration of event i. You may assume that 1 ≤ Si ≤ 1000000 and 1 ≤ Di ≤ 1000.

The range of values over which your program is to be tested is mentioned above. In addition, 50% of the test cases will also satisfy N ≤ 10000.

输出

Your output must consist of a single line containing a single integer M, indicating the maximum possible number of events that can use the stadium.

样例输入

4
2 5
9 7
15 6
9 3

样例输出

3

题意

有一个体育场,给你n个项目的开始和结束时间,求最多可以安排多少项目

题解

区间贪心,在可选的事件中,每次都选取结束时间最早的事件

代码

 1 #include<stdio.h>
 2 #include<algorithm>
 3 using namespace std;
 4 struct t{
 5     int s,e;
 6 }a[100005];
 7 bool cmp(t a,t b){
 8     return a.e<b.e;
 9 }
10 int main(){
11     int i,d,n;
12     scanf("%d",&n);
13     for(i=0;i<n;i++){
14         scanf("%d%d",&a[i].s,&d);
15         a[i].e=a[i].s+d-1;
16     }
17     sort(a,a+n,cmp);
18     int t=0,ans=0;
19     for(i=0;i<n;++i){
20         if(a[i].s>t){
21             ans++;
22             t=a[i].e;
23         }
24     }
25     printf("%d\n",ans);
26     return 0;
27 }

转载于:https://www.cnblogs.com/taozi1115402474/p/8310122.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值