bzoj 1664 (贪心)
[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
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
就是这里的,想象成许多线段,如果相交,若当前线段右端比以前线段右端小,那么就赋值为现在那条线段,因为这样只会更优。
如果不想交,直接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 (贪心)的更多相关文章
- bzoj 1193 贪心
如果两点的曼哈顿距离在一定范围内时我们直接暴力搜索就可以得到答案,那么开始贪心的跳,判断两点横纵坐标的差值,差值大的方向条2,小的条1,不断做,直到曼哈顿距离较小时可以暴力求解. 备注:开始想的是确定 ...
- bzoj 2697 贪心
就贪心就行了,首先可以看成n个格子,放物品,那么 一个物品假设放3个,放在1,k,n处,那么价值和放在1,n 是一样的,所以一个物品只放两个就行了,价值大的应该尽量放 在两边,那么排序之后模拟就行了 ...
- bzoj 3037 贪心
我们可以贪心的分析,每个点的入度如果是0,那么这个点不可能 被用来更新答案,那么我们每次找入度为0的点,将他去掉,如果他连的 点没有被更新过答案,那么更新答案,去掉该点,环的时候最后处理就行了 /** ...
- BZOJ 1664: [Usaco2006 Open]County Fair Events 参加节日庆祝( dp )
先按时间排序( 开始结束都可以 ) , 然后 dp( i ) = max( dp( i ) , dp( j ) + 1 ) ( j < i && 节日 j 结束时间在节日 i 开 ...
- bzoj 1193 贪心+bfs
1193: [HNOI2006]马步距离 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2015 Solved: 914[Submit][Statu ...
- bzoj 1899 贪心+dp
思路:这个贪心排顺序我居然没看出来. 吃饭时间长的在前面, 用反证法很容易得出. 剩下的就是瞎dp啦. #include<bits/stdc++.h> #define LL long lo ...
- 【题解】期末考试 六省联考 2017 洛谷 P3745 BZOJ 4868 贪心 三分
题目传送门:这里是萌萌哒传送门(>,<) 啊♀,据说这题有个完全贪心的做法,但是要维护太多东西好麻烦的(>,<),于是就来口胡一发三分的做法. 思路很简单,假设我指定了一个x, ...
- bzoj 1029 贪心
贪心的一种,维护一种尽可能优的状态(即不会比最优解差),将这种状态保持到最后. /*********************************************************** ...
- bzoj 1034 贪心
首先如果我们想取得分最高的话,肯定尽量赢,实在赢不了的话就耗掉对方最高的,那么就有了贪心策略,先排序,我方最弱的马和敌方最弱的相比,高的话赢掉,否则耗掉敌方最高的马. 对于一场比赛,总分是一定的,所以 ...
随机推荐
- ASP.Net 知识点总结(五)
1.传入某个属性的set方法的隐含参数的名称是什么?value,它的类型和属性所声名的类型相同.2.如何在C#中实现继承? 在类名后加上一个冒号,再加上基类的名称.3.C#支持多重继承么? 不支持.可 ...
- PowerDesigner 的使用教程
PowerDesigner 的使用这两篇博客挺好,我也是跟着学习,就不再写了: 初步学习: http://www.cnblogs.com/huangcong/archive/2010/06/14/17 ...
- 【hdu多校联考第二场】Odd Shops
Description 这道题的题意是这道难读,大概就是给你n个商店,每个商店的重量为i的商品用ai表示,对于任意商店的a数列都是相同的,重量的范围为[1,10] 求购买方案总数为奇数的重量一共有多少 ...
- 题解报告:poj 1094 Sorting It All Out(拓扑排序)
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- Kafka~服务端几个常用的命令
在Centos上安装和部署完成kafka之后,我们就可以向服务端推消息和消费它了,今天主要聊几个常用的命令,比建立topic,从broken显示所有topics列表,向broken发消息,从broke ...
- 在dataGridView空间中添加数据
//查询信息sql语句 string sql = "select studentName,addres from student"; SqlDataAdapter adapter ...
- SQL在一张表中根据父ID获取所有的子ID
with a as ( select id,name,parentid from categories where id=53 union all select x.id,x.name,x.paren ...
- Spring.Net学习笔记(3)-创建对象
一.开发环境 编译器:VS2013 .Net版本:.net framework4.5 二.涉及程序集 Spring.Core.dll:1.3 Common.Logging 三.开发过程 1.项目结构 ...
- 一个JavaScript贷款计算器
通过本案例,将会学到: . 如何在文档中查找元素 . 如何通过表单input元素来获取用户的输入数据 . 如何通过文档元素来设置HTML内容 . 如何将数据存储在浏览器中 . 如何使用脚本发起HTTP ...
- ios基础笔试题-集锦二
前言 下文转载自:http://www.henishuo.com/objc-interview-two/ 1.即时聊天App不会采用的网络传输方式 A. UDP B. TCP C. HTTP D. F ...