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 贪心
首先如果我们想取得分最高的话,肯定尽量赢,实在赢不了的话就耗掉对方最高的,那么就有了贪心策略,先排序,我方最弱的马和敌方最弱的相比,高的话赢掉,否则耗掉敌方最高的马. 对于一场比赛,总分是一定的,所以 ...
随机推荐
- VUE学习之计算属性computed
计算属性:computed 先看一下官网的说法 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div id="ex ...
- 源码阅读之LinkedHashMap(JDK8)
概述 LinkedHashMap继承自HashMap,实现了Map<K,V>接口.其内部还维护了一个双向链表,在每次插入数据,或者访问.修改数据时,会增加节点.或调整链表的节点顺序.以决定 ...
- 【题解】二逼平衡树 [P3380] [BZOJ3196] [Tyvj1730]
[题解]二逼平衡树 [P3380] [BZOJ3196] [Tyvj1730] 传送门:[模板]二逼平衡树(树套树)\([P3380]\) \([BZOJ3196]\) \([TYVJ1730]\) ...
- 微信公众号 sign类
微信公众号 Sign import java.util.UUID; import java.util.Map; import java.util.HashMap; import java.util.F ...
- Visual Studio 相关
基础配置: 背景色:豆沙绿(色调84 饱和度118 亮度205) 字体字号:Consolas 11号 离线下载方法: vs_enterprise.exe --layout c:\vs2017offl ...
- 窗体基础WINFORM
winform 1.窗体: 造窗体界面: 窗体设计界面: 窗体类名不能重复! 属性: acceptbutton:回车是默认点击按钮 cancelbutton:按esc按键式默认的按钮 backcolo ...
- SQL数据库--数据访问
数据访问: 对应命名空间:System.Data.SqlClient; SqlConnection:连接对象 SqlCommand:命令对象 SqlDataReader:读取器对象 //造连接字符串 ...
- SVN服务器搭建 内网可用外网不可用的问题
检查1:内网端口映射到了外网端口,这样外网才能够访问到 映射的方式有两种: 1.通过路由器的虚拟服务器功能,网上一搜一大把. 2.将路由器的DMZ功能开启,并把DMZ主机设置为目标计算机. 检查2:S ...
- (8)string对象上的操作1
读写操作 //读写string对象的测试.//本程序输入两string类,输出两string类. #include <iostream> #include <string> ...
- POJ_2255_Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12342 Accepted: 7712 De ...