描述

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[];
  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=;i<n;i++){
  14. scanf("%d%d",&a[i].s,&d);
  15. a[i].e=a[i].s+d-;
  16. }
  17. sort(a,a+n,cmp);
  18. int t=,ans=;
  19. for(i=;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 ;
  27. }

TZOJ 4007 The Siruseri Sports Stadium(区间贪心)的更多相关文章

  1. HDU 1936 区间贪心

    /* *区间贪心.前几天刚做了POJ 1328 ...思路完全相同... *最多有100个表情,100行文字.遍历寻找每个表情的所在区间.时间复杂度大约在10^5 ~ 10^6 可以接受. *然后对每 ...

  2. HDU 2037 今年暑假不AC (区间贪心)

    题意:又是中文题... 析:先说一下区间贪心的一个定理,选择不相交的区间:数轴上有n个开区间(ai, bi).选择尽量多的区间,使得这些区间两两不相交,贪心策略是,一定是选bi小的.(想一下为什么). ...

  3. UVA-11134 Fabled Rooks 贪心问题(区间贪心)

    题目链接:https://cn.vjudge.net/problem/UVA-11134 题意 在 n*n 的棋盘上,放上 n 个车(ju).使得这 n 个车互相不攻击,即任意两个车不在同一行.同一列 ...

  4. 【题解】P1712 [NOI2016]区间(贪心+线段树)

    [题解]P1712 [NOI2016]区间(贪心+线段树) 一个observe是,对于一个合法的方案,将其线段长度按照从大到小排序后,他极差的来源是第一个和最后一个.或者说,读入的线段按照长度分类后, ...

  5. 贪心思想之区间贪心 关联洛谷P1803

    力扣上也有一道类似的题 几乎是一样 输出不同 → 力扣leetcode 435. 无重叠区间 区间贪心是比较经典的 就拿洛谷P1803来举例 题目大意 n个比赛 [开始时间,结束时间] 问一个人最多能 ...

  6. 力扣leetcode 435. 无重叠区间 - 贪心

    非常经典的区间贪心思想 -- 详见博文: 贪心思想之区间贪心 本题给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] ...

  7. [BZOJ4653][NOI2016]区间 贪心+线段树

    4653: [Noi2016]区间 Time Limit: 60 Sec  Memory Limit: 256 MB Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],. ...

  8. poj2376 Cleaning Shifts 区间贪心

    题目大意: (不说牛了) 给出n个区间,选出个数最少的区间来覆盖区间[1,t].n,t都是给出的. 题目中默认情况是[1,x],[x+1,t]也是可以的.也就是两个相邻的区间之间可以是小区间的右端与大 ...

  9. Radar Installation(POJ 1328 区间贪心)

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 68578   Accepted: 15 ...

随机推荐

  1. Linux下查看某个进程占用的CPU、内存

    1.用top命令指定固定的PID top -p 10997 查询指定进程的PID ps -ef | grep zookeeper jim 10997 1959 0 12月14 pts/2 00:00: ...

  2. Linux常用的一些基础命令

    删除 rm -rvf * -f:强制删除文件或文件夹 -r:递归的删除文件或文件夹 -i:删除文件或文件夹前需要确认 -v:详细显示进行步骤 查看 ls ll        ls -l cat mor ...

  3. sql编程中流程控制 IF ……THEN……ELSEIF……THEN………END IF

    写mysql存储过程应注意的几点: 1.声明变量(declare)时要注意字符集,用变量存储表字段时,表字段与变量的字符编码要一致. 2.mysql的字符合并不能用‘+’号,必须用concat函数. ...

  4. Kafka 基本原理

    Kafka 基本原理   来源:阿凡卢 , www.cnblogs.com/luxiaoxun/p/5492646.html 简介 Apache Kafka是分布式发布-订阅消息系统.它最初由Link ...

  5. docker 配置pull源

    登录阿里云 找到镜像服务 在/etc/docker下创建daemon.json文件并写入 最后重启docker服务 sudo service docekr restart

  6. 有关Firefox/Chrome的问题汇总

    安装的附加组件因未经验证而被 Firefox 禁用,我该怎么办 如果您已安装的附加组件因未经验证而被禁用了,建议您联系附加组件开发者或提供给您附加组件的人,看看他们能不能提供新版经过验证的附加组件.您 ...

  7. 谈谈 Python 程序的运行原理

    因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,谈谈 Python 程序的运行原理 这篇文章准确说是『Python 源码剖析』的 ...

  8. 拒绝网页被 iframe 嵌套

    在响应头里加一个X-Frame-Options DENY:浏览器拒绝当前页面加载任何Frame页面 SAMEORIGIN:frame页面的地址只能为同源域名下的页面 ALLOW-FROM origin ...

  9. 未能同步 iPhone XXX,因为这台电脑不再被授权使用在此iPhone上购买的项目。

    打包生成的ipa文件,安装到手机上,p12和ppf证书都正确,手机的udid也正确.用itunes安装到手机报错. 未能同步 iPhone XXX,因为这台电脑不再被授权使用在此iPhone上购买的项 ...

  10. Ztree学习(-)简单例子

    https://www.cnblogs.com/shinhwazt/p/5828031.html ztree包:https://pan.baidu.com/s/1vOgGm_elF-lF0VowoHw ...