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 贪心
首先如果我们想取得分最高的话,肯定尽量赢,实在赢不了的话就耗掉对方最高的,那么就有了贪心策略,先排序,我方最弱的马和敌方最弱的相比,高的话赢掉,否则耗掉敌方最高的马. 对于一场比赛,总分是一定的,所以 ...
随机推荐
- Android框架式编程之EasyPermissions
EasyPermission库是一个谷歌官方提供的简化基本的系统权限逻辑的库,可用于在Android M或者更高版本上. 官方项目地址:https://github.com/googlesamples ...
- ACM_下一个排列
The Next Permutation Time Limit: 2000/1000ms (Java/Others) Problem Description: For this problem, yo ...
- Too many open files故障解决一例
Linux环境WebSphere输出日志: [// ::: EDT] 000090b7 SystemErr R Caused by: java.io.FileNotFoundException: /o ...
- (9)string对象上的操作2
比较string对象的比较运算符 这种由string类定义的几种比较字符串的运算符能逐一比较string对象中的字符(对大小写敏感).
- 生产环境4.3.5jboss内存调优
1.查看jboss的监控工具 http://XXX/jmx-console/htmladaptor 2.查看jvm的监控工具 jdk\bin jvisualvm.exe jmc.exe 3.查看jbo ...
- 3星|《未来公司》:Uber简史
未来公司(卡兰尼克和他的Uber帝国)(精) 从Uber创始人卡兰尼克的角度讲的Uber简史,截止到2017年.书中有不少Uber的负面新闻,比国内的同类书要好. 基本是流水账.想了解这家公司的历史, ...
- CMU Database Systems - Two-phase Locking
首先锁是用来做互斥的,解决并发执行时的数据不一致问题 如图会导致,不可重复读 如果这里用lock就可以解决,数据库里面有个LockManager来作为master,负责锁的记录和授权 数据库里面的基本 ...
- mysql导出数据到excel的两种方式
使用第一种方式如果数据中有换行符的话会自动换行,但使用第二种方式就不会出现这种效果了.两种方式自己选择哈 1:select * from into outfile 'c:/Users/a.xls' t ...
- P2041 分裂游戏
P2041 分裂游戏 手推$n=3$是无解的,推断$n>=3$是无解的 证明略,这是道结论题. #include<iostream> #include<cstdio> # ...
- 「 COGS 2240 」 X 「 Luogu P2885 」 架设电话线
解题思路 首先很容易就想到了一个二维的朴素的 $dp$. 设 $dp[i][j]$ 表示第 $i$ 个位置的电话线杆的高度为 $j$ 时的最小花费,就需要枚举第 $i$ 个电话线杆.第 $i$ 个电话 ...