BZOJ 3433 [Usaco2014 Jan]Recording the Moolympics:贪心
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3433
题意:
给出n个区间[a,b)。
有两个记录器,每个记录器中存放的区间不能重叠。
求两个记录器中最多可放多少个区间。
题解:
贪心。
先按右端点从小到大排序。
p1,p2分别为两个记录器的最右端。
始终令p1 < p2(避免出现大材小用)。
对于每个区间s:
if p1 <= s.l,则将s放入p1所在记录器。即:p1 = s.r, ans++;
else if p2 <= s.l,则将s放入p1所在记录器。即:p2 = s.r, ans++;
else 那就没法放了...
AC Code:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define MAX_N 155
#define INF 1000000000 using namespace std; struct Sec
{
int l;
int r;
Sec(int _l,int _r)
{
l=_l;
r=_r;
}
Sec(){}
friend bool operator < (const Sec &a,const Sec &b)
{
return a.r!=b.r?a.r<b.r:a.l<b.l;
}
}; int n;
int ans=;
Sec s[MAX_N]; void read()
{
cin>>n;
for(int i=;i<n;i++)
{
cin>>s[i].l>>s[i].r;
}
} void solve()
{
sort(s,s+n);
int p1=-INF;
int p2=-INF;
for(int i=;i<n;i++)
{
if(p1<p2) swap(p1,p2);
if(p1<=s[i].l)
{
ans++;
p1=s[i].r;
}
else if(p2<=s[i].l)
{
ans++;
p2=s[i].r;
}
}
} void print()
{
cout<<ans<<endl;
} int main()
{
read();
solve();
print();
}
BZOJ 3433 [Usaco2014 Jan]Recording the Moolympics:贪心的更多相关文章
- 3433: [Usaco2014 Jan]Recording the Moolympics
3433: [Usaco2014 Jan]Recording the Moolympics Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 137 S ...
- 【BZOJ】3433: [Usaco2014 Jan]Recording the Moolympics (贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=3433 想了好久啊....... 想不出dp啊......sad 后来看到一英文题解......... ...
- BZOJ3433: [Usaco2014 Jan]Recording the Moolympics
3433: [Usaco2014 Jan]Recording the Moolympics Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 55 So ...
- 【bzoj 3433】{Usaco2014 Jan} Recording the Moolympics(算法效率--贪心)
题意:给出n个区间[a,b),有2个记录器,每个记录器中存放的区间不能重叠.求2个记录器中最多可放多少个区间. 解法:贪心.只有1个记录器的做法详见--关于贪心算法的经典问题(算法效率 or 动态规划 ...
- BZOJ 3430: [Usaco2014 Jan]Ski Course Rating(并查集+贪心)
题面 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 136 Solved: 90 [Submit][Status][Discuss] Descript ...
- bzoj 1783: [Usaco2010 Jan]Taking Turns【贪心+dp】
不知道该叫贪心还是dp 倒着来,记f[0][i],f[1][i]分别为先手和后手从n走到i的最大值.先手显然是取最大的,当后手取到比先手大的时候就交换 #include<iostream> ...
- BZOJ 3432: [Usaco2014 Jan]Cross Country Skiing (二分+染色法)
还是搜索~~可以看出随着D值的增大能到达的点越多,就2分d值+染色法遍历就行啦~~~ CODE: #include<cstdio>#include<iostream>#incl ...
- [bzoj 3048] [Usaco2013 Jan]Cow Lineup
[bzoj 3048] [Usaco2013 Jan]Cow Lineup Description 给你一个长度为n(1<=n<=100,000)的自然数数列,其中每一个数都小于等于10亿 ...
- BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花( 贪心 )
考虑相邻的两头奶牛 a , b , 我们发现它们顺序交换并不会影响到其他的 , 所以我们可以直接按照这个进行排序 ------------------------------------------- ...
随机推荐
- Jquery 中Ajax使用的四种情况
<script type="text/javascript" language="javascript" src="JS/jquery-1[1] ...
- 如何自定义View
1. 首先 在values目录下建立attrs.xml文件,添加属性内容 ·在布局文件中添加新的命名空间xmlns,然后可以使用命名空间给自定义的空间设置属性 attrs.xml <resour ...
- 推荐一个android 日期时间选择器(转)
最近接触了日期选择的功能,那么肯定得需要一个日期选择控件,Android 系统有自带的 DatePicker 控件,但是不说这个控件有多 难看吧,现在 Android 手机版本那么多,用户弹出来的控件 ...
- ps -ef 和 aux 区别
Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...
- APNS的推送机制 3
APNS的推送机制 首先我们看一下苹果官方给出的对iOS推送机制的解释.如下图 Provider就是我们自己程序的后台服务器,APNS是Apple Push Notification Service的 ...
- Boxes and Candies(贪心)
Boxes and Candies Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Ther ...
- .net EF监控 MiniProfiler
1.从NuGet上下载所需要的包:MiniProfiler.mvc,MiniProfiler,MiniProfiler.ef 2.Global.asax 加入 protected void Appli ...
- can t connect to mysql server on 'localhost'解决方法
源:http://www.111cn.net/database/mysql/37700.htm 先重启服务器可解决大部分问题 错误编号:2003 问题分析: 无法连接到 mysql 服务器,可能的情况 ...
- MyBatis -- 一步步教你使用MyBatis
1.建立开发环境 1.1 创建项目,java项目或者javaweb项目均可,如图: 1.2 加入所须要的jar包到项目lib文件夹下 一个MyBatis-3.2.4.jar包 一个驱动包mysql ...
- Render a controller in Twig - Unexpected “render” tag - expecting closing tag for the “block” tag defined
Render a controller in Twig - Unexpected “render” tag - expecting closing tag for the “block” tag de ...