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



INPUT DETAILS:



Graphic picture of the schedule:

11111111112

12345678901234567890---------这个是时间轴.

--------------------

111111 2222223333344

55555555 777777 666



这个图中1代表第一个节日从1开始,持续6个时间,直到6.


Sample Output

4



OUTPUT DETAILS:



FJ can do no better than to attend events 1, 2, 3, and 4.

我会n^2的算法耶……幸好数据弱

首先把每个事件的开始时间、结束时间提出来快排,然后令f[i]表示快排后前i个最多能取多少个,枚举如果f[j].t<f[i].s,那么事件j一定在i前面,就可以用j来更新答案

其实注意到if (e[j].t<e[i].s) f[i]=max(f[i],f[j]+1)这一行,显然可以用平衡树加速,但是我很懒,又不会STL的set,就不打了

#include<cstdio>
#include<algorithm>
using namespace std;
struct event{
int s,t;
}e[10010];
int n;
int f[10010];
inline bool cmp(const event &a,const event &b)
{return a.s<b.s||a.s==b.s&&a.t<b.t;}
inline int max(int a,int b)
{return a>b?a:b;}
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int main()
{
n=read();
for (int i=1;i<=n;i++)
{
e[i].s=read();
e[i].t=e[i].s+read()-1;
}
sort(e+1,e+n+1,cmp);
for(int i=1;i<=n;i++)
{
f[i]=1;
for (int j=1;j<i;j++)
if (e[j].t<e[i].s) f[i]=max(f[i],f[j]+1);
}
printf("%d",f[n]);
}

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

  1. 【动态规划】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 ...

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

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

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

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

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

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

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

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

  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. Sans Serif 与 Serif 字体是什么意思?

    在西方国家罗马字母阵营中,字体分为两大种类:Sans Serif和 Serif,打字机体虽然也属于 Sans Serif,但由于是等宽字体,所以另外独立出 Monospace 这一种类,例如在Web中 ...

  2. kibana 统计每天注册数

  3. POJ 2752 Seek the Name, Seek the Fame (KMP next 数组 变形)

    题意:给一个字符串S,判断在什么下标的时候,前缀和后缀相等,输出前缀和后缀相等的点. 分析:next数组的一种很巧妙的用法 next数组表示的意义是当前下标前面k字符和开头的前面k个字符相等 所以就会 ...

  4. iOS 按住Home键的代理

    1.按住home键怎么保存程序的运行状态: 监听按home键的时候程序挂起的状态(通知),然后让其重新进入程序[[NSNotificationCenter defaultCenter] addObse ...

  5. 批量设置AssetBundleName

    using UnityEngine; using System.Collections; using UnityEditor; using System.IO; public class Change ...

  6. STL中map,set的基本用法示例

    本文主要是使用了STL中德map和set两个容器,使用了它们本身的一些功能函数(包括迭代器),介绍了它们的基本使用方式,是一个使用熟悉的过程. map的基本使用: #include "std ...

  7. UIProgressView-初识IOS

    好几天没更新了,学的时候太紧,没时间复习了都.今天刚好有时间,多更几个. 今天复习的是UIProgressView,我们常见使用在修改某些属性的时候经常用到,比如透明度,今天我们介绍一个简单的使用例子 ...

  8. 推荐几个常用的jquery ui 框架

    jQuery ui框架很多,除了官方提供的jquery UI(如果你还不知道什么是jQuery UI,请看下载了jquery ui后如何使用),还有很多第三方提供的ui框架,因官方提供的jquery ...

  9. vs2013 JS代码提示

    1.JS提示 在Js文件头部加 /// <reference path="ext-all-dev.js" /> 要求引用的js和本js在同一目录,否则需要全部路径

  10. Android 使用图标字体库

    首先,在assets资源文件下放入图标字体库. 我这儿采用的是fontawesome-webfont.ttf 然后, 在安卓中加载这个资源文件 Typeface fontFace = Typeface ...