Description

Farmer John has returned to the County Fair so he can attend the special events (concerts, rodeos, cooking shows, etc.). He wants to attend as many of the N (1 <= N <= 10,000) special events as he possibly can. He's rented a bicycle so he can speed from one event to the next in absolutely no time at all (0 time units to go from one event to the next!). Given a list of the events that FJ might wish to attend, with their start times (1 <= T <= 100,000) and their durations (1 <= L <= 100,000), determine the maximum number of events that FJ can attend. FJ never leaves an event early.

有N个节日每个节日有个开始时间,及持续时间. 牛想尽可能多的参加节日,问最多可以参加多少. 注意牛的转移速度是极快的,不花时间.

Input

  • Line 1: A single integer, N.

  • Lines 2..N+1: Each line contains two space-separated integers, T and L, that describe an event that FJ might attend.

Output

  • Line 1: A single integer that is the maximum number of events FJ can attend.

Sample Input

7

1 6

8 6

14 5

19 2

1 8

18 3

10 6

Sample Output

4


首先按结束时间排序,然后一路搜下去,发现一个节日的开始时间在所记录的结束时间之后,就参加这个节日,同时更新记录的结束时间(典型贪心思想)

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline void print(int x){
if (x>=10) print(x/10);
putchar(x%10+'0');
}
const int N=1e4;
struct AC{
int l,r;
void join(int x,int y){l=x,r=y;}
bool operator <(const AC &x)const{return r<x.r;}
}A[N+10];
int main(){
int n=read();
for (int i=1,x,y;i<=n;i++) x=read(),y=read(),A[i].join(x,x+y);
sort(A+1,A+1+n);
int x=A[1].r,ans=1;
for (int i=2;i<=n;i++) if (A[i].l>=x) x=A[i].r,ans++;
printf("%d\n",ans);
return 0;
}

[Usaco2006 Open]County Fair Events 参加节日庆祝的更多相关文章

  1. BZOJ 1664: [Usaco2006 Open]County Fair Events 参加节日庆祝( dp )

    先按时间排序( 开始结束都可以 ) , 然后 dp( i ) = max( dp( i ) , dp( j ) + 1 ) ( j < i && 节日 j 结束时间在节日 i 开 ...

  2. 1664: [Usaco2006 Open]County Fair Events 参加节日庆祝

    1664: [Usaco2006 Open]County Fair Events 参加节日庆祝 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 255  S ...

  3. bzoj1664 [Usaco2006 Open]County Fair Events 参加节日庆祝

    Description Farmer John has returned to the County Fair so he can attend the special events (concert ...

  4. 【BZOJ】1664: [Usaco2006 Open]County Fair Events 参加节日庆祝(线段树+dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1664 和之前的那题一样啊.. 只不过权值变为了1.. 同样用线段树维护区间,然后在区间范围内dp. ...

  5. 【动态规划】bzoj1664 [Usaco2006 Open]County Fair Events 参加节日庆祝

    将区间按左端点排序. f(i)=max{f(j)+1}(p[j].x+p[j].y<=p[i].x && j<i) #include<cstdio> #incl ...

  6. bzoj 1664: [Usaco2006 Open]County Fair Events 参加节日庆祝【dp+树状数组】

    把长度转成右端点,按右端点排升序,f[i]=max(f[j]&&r[j]<l[i]),因为r是有序的,所以可以直接二分出能转移的区间(1,w),然后用树状数组维护区间f的max, ...

  7. County Fair Events

    先按照结束时间进行排序,取第一个节日的结束时间作为当前时间,然后从第二个节日开始搜索,如果下一个节日的开始时间大于当前的时间,那么就参加这个节日,并更新当前时间 #include <bits/s ...

  8. BZOJ-USACO被虐记

    bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...

  9. 小结:线段树 & 主席树 & 树状数组

    概要: 就是用来维护区间信息,然后各种秀智商游戏. 技巧及注意: 一定要注意标记的下放的顺序及影响!考虑是否有叠加或相互影响的可能! 和平衡树相同,在操作每一个节点时,必须保证祖先的tag已经完全下放 ...

随机推荐

  1. Meteor Assets资源

    静态服务器资源位于应用程序内的 private 子文件夹.在这个例子中,我们将学习如何从简单的JSON文件中使用数据. 第1步 - 创建文件和文件夹 让我们创建一个 private 文件夹并在这个文件 ...

  2. 一起talk C栗子吧(第九十六回:C语言实例--使用共享内存进行进程间通信二)

    各位看官们.大家好,上一回中咱们说的是使用共享内存进行进程间通信的样例,这一回咱们接着上一回内容继续说使用共享内存进行进程间通信. 闲话休提,言归正转.让我们一起talk C栗子吧! 我们在上一回中介 ...

  3. Linux下获取线程TID的方法

    如何获取进程的PID(process ID)? 可以使用: #include <unistd.h> pid_t getpid(void); 通过查看头文件说明,可以得到更详细的信息: fi ...

  4. &lt;一&gt;读&lt;&lt;大话设计模式&gt;&gt;之简单工厂模式

    工厂模式尽管简单.可是写下这篇文章却不简单. 第一:本人经过内心的挣扎后才决定開始写博文的.为什么呢,由于好长时间没有写了,对自己的文学功底也是好不自信.可是技术这东西你不写出来你真不知道自己掌握多少 ...

  5. BZOJ 1005 明明的烦恼 Prufer序列+组合数学+高精度

    题目大意:给定一棵n个节点的树的节点的度数.当中一些度数无限制,求能够生成多少种树 Prufer序列 把一棵树进行下面操作: 1.找到编号最小的叶节点.删除这个节点,然后与这个叶节点相连的点计入序列 ...

  6. soapUI系列之—-03 Groovy脚本常用方法2

    ------Groovy脚本常用方法 1.解析Json数据脚本 //groovy读取json的方式很简单,re.body.businessinfo.c2rate读取c2rate对应的值 import ...

  7. mysql字段去重方式

    一直找不出某个字段去重的前提下,还能够显示其它字段的数据 以下是解决方法: SELECT *, COUNT(DISTINCT( province)) FROM area_info WHERE type ...

  8. uboot中关于LCD的代码分析【转】

    本文转载自:http://blog.csdn.net/oqqHuTu12345678/article/details/72236117 以下内容源于朱有鹏<物联网大讲坛>课程的学习,如有侵 ...

  9. UVA315 Network —— 割点

    题目链接:https://vjudge.net/problem/UVA-315 A Telephone Line Company (TLC) is establishing a new telepho ...

  10. android 自定义View开发实战(六) 可拖动的GridView

    1前言 由于项目需求,需要把项目的主界面采用GridView显示,并且需要根据模块优先级支持拖动图标(砍死产品狗).为此,自定义了一个支持拖拽图标的GridView.效果如下: 具体效果如上图 2 可 ...