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

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 487  Solved: 344
[Submit][Status][Discuss]

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
 
这里很容易贪心证明,不需要去O(n^2)去跑,很容易被卡。

就是这里的,想象成许多线段,如果相交,若当前线段右端比以前线段右端小,那么就赋值为现在那条线段,因为这样只会更优。

如果不想交,直接ans+1

划水

 #include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstdio> #define N 10007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while(ch<=''&&ch>='')
{
x=(x<<)+(x<<)+ch-'';
ch=getchar();
}
return x*f;
} int n,ans;
struct Node
{
int x,y;
}a[N]; bool cmp(Node x,Node y)
{
return x.x<y.x;
}
int main()
{
n=read();
for (int i=;i<=n;a[i].x=read(),a[i].y=a[i].x+read()-,i++);
sort(a+,a+n+,cmp); for (int i=,r=;i<=n;i++)
{
if (a[i].x>r) ans++,r=a[i].y;
else if (a[i].y<r) r=a[i].y;
}
printf("%d",ans);
}

bzoj 1664 (贪心)的更多相关文章

  1. bzoj 1193 贪心

    如果两点的曼哈顿距离在一定范围内时我们直接暴力搜索就可以得到答案,那么开始贪心的跳,判断两点横纵坐标的差值,差值大的方向条2,小的条1,不断做,直到曼哈顿距离较小时可以暴力求解. 备注:开始想的是确定 ...

  2. bzoj 2697 贪心

    就贪心就行了,首先可以看成n个格子,放物品,那么 一个物品假设放3个,放在1,k,n处,那么价值和放在1,n 是一样的,所以一个物品只放两个就行了,价值大的应该尽量放 在两边,那么排序之后模拟就行了 ...

  3. bzoj 3037 贪心

    我们可以贪心的分析,每个点的入度如果是0,那么这个点不可能 被用来更新答案,那么我们每次找入度为0的点,将他去掉,如果他连的 点没有被更新过答案,那么更新答案,去掉该点,环的时候最后处理就行了 /** ...

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

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

  5. bzoj 1193 贪心+bfs

    1193: [HNOI2006]马步距离 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2015  Solved: 914[Submit][Statu ...

  6. bzoj 1899 贪心+dp

    思路:这个贪心排顺序我居然没看出来. 吃饭时间长的在前面, 用反证法很容易得出. 剩下的就是瞎dp啦. #include<bits/stdc++.h> #define LL long lo ...

  7. 【题解】期末考试 六省联考 2017 洛谷 P3745 BZOJ 4868 贪心 三分

    题目传送门:这里是萌萌哒传送门(>,<) 啊♀,据说这题有个完全贪心的做法,但是要维护太多东西好麻烦的(>,<),于是就来口胡一发三分的做法. 思路很简单,假设我指定了一个x, ...

  8. bzoj 1029 贪心

    贪心的一种,维护一种尽可能优的状态(即不会比最优解差),将这种状态保持到最后. /*********************************************************** ...

  9. bzoj 1034 贪心

    首先如果我们想取得分最高的话,肯定尽量赢,实在赢不了的话就耗掉对方最高的,那么就有了贪心策略,先排序,我方最弱的马和敌方最弱的相比,高的话赢掉,否则耗掉敌方最高的马. 对于一场比赛,总分是一定的,所以 ...

随机推荐

  1. bzoj 1726: [Usaco2006 Nov]Roadblocks第二短路【dijskstra】

    严格次短路模板,用两个数组分别维护最短路和次短路,用dijskstra,每次更新的时候先更新最短路再更新次短路 写了spfa版的不知道为啥不对-- #include<iostream> # ...

  2. 搞定springboot项目连接远程服务器上kafka遇到的坑以及完整的例子

    版本 springboot 2.1.5.RELEASE kafka 2.2 遇到的坑 用最新的springboot就要用最新的kafka版本! 当我启动云服务器上的zk后,再启动kafka后台日志也没 ...

  3. linux守护进程的编写

    linux监控一个进程进行 代码如下: #!/bin/sh cd /home/autoprocess/ auto=`pgrep -f autoProcessNew.php | wc -l` if [ ...

  4. hdu6198 number number number(递推公式黑科技)

    number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. 题解报告:hdu 1035 Robot Motion(简单搜索一遍)

    Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...

  6. datagrid 选中某行,翻页再翻回来,发现选中的行没有选中

    不管有没有设置复选框,其实都是一样的,都是idField属性没有设置,加上去即可. $(function(){ $('#dg').datagrid({ url:'ContactServlet', to ...

  7. 类支付宝密码输入框NumberEditText(简单粗暴的定制方式)

    因为项目需要,设计了一个下图样的验证码输入框(ps:个人认为还不如直接一个EditText,用户友好度可能更好,何况这页面99.9%的用户不会使用,但是没办法,别人才是专业的设计师). 其实界面很简单 ...

  8. PMBOK项目管理九大知识领域和五大流程 --美国IT项目管理硕士笔记(二)

    PMBOK 项目管理 九大知识领域和五大流程 PMI   Project Management Institute.PMI 是世界上最大的非盈利机构,是项目管理领域的领导者.PMI制定项目管理行业标准 ...

  9. APP上线被APPStore拒绝的各种原因

    1.程序有重大bug,程序不能启动,或者中途退出.2.绕过苹果的付费渠道,我们之前游戏里的用兑换码兑换金币.3.游戏里有实物奖励的话,一定要说清楚,奖励由本公司负责,和苹果没有关系.4.用到苹果的标志 ...

  10. 没搞错吧,我只是个web前端工程师,不是manager,也不是leader...

    那个时候,我只想好好的学习web前端技术,恨不得把有限的时间和精力都放在提升技术上. 然而,让自己在坑里茁壮成长,要先适应坑内的环境. 首当其冲我们要弄明白的事情有: 团队成员的技术能力和状态 Lea ...