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 , 我们发现它们顺序交换并不会影响到其他的 , 所以我们可以直接按照这个进行排序 ------------------------------------------- ...
随机推荐
- python 的三元表达式
python中的三目运算符不像其他语言 其他的一般都是 判定条件?为真时的结果:为假时的结果 如 result=5>3?1:0 这个输出1,但没有什么意义,仅仅是一个例子. 而在python中的 ...
- Linux安装php-7.0.16,完成php和apache的配置
Linux安装php-7.0.16,完成php和apache的配置 版本:php-7.0.16.tar.gz,libxml2-2.9.2.tar.gz(php需要它的支持,首先安装它) 说明 ...
- apache重定向无效
这个问题让我纠结了蛮久啊,因为之前一直不注意SEO,网站带www和不带www的一级域名都被收录了,而且不知道为什么不带www的一级域名被收录比www还多,这可不是我的初衷!这次吸取教训了,以后再开站不 ...
- LeetCode78:Subsets
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- /etc/cron.d添加定时任务脚本后不生效
原因:定时任务脚本中的命令中包含了环境变量,crontab不能读取到环境变量. vim /etc/cron.d/mymon #mymon内容如下: * * * * * root cd $GOPATH/ ...
- 初识Modbus TCP/IP-------------C#编写Modbus TCP客户端程序(二)
由于感觉上一次写的篇幅过长,所以新开一贴,继续介绍Modbus TCP/IP的初步认识, 书接上回 3).03(0x03)功能码--------读保持寄存器 请求与响应格式 这是一个请求读寄存器108 ...
- Node-Webkit - package.json - 参数设置
必填: main :(string)APP的主入口,指定一个html文件,如:main:"index.htm". name :(string)APP的名称,必须具有唯一性. 例子: ...
- shu7-19【背包和母函数练习】
题目:http://acm.hdu.edu.cn/diy/contest_show.php?cid=20083 密码:shuacm 感觉他们学校的新生训练出的比较好. 今天很多题目都是强化了背包的转化 ...
- Centos设置开机启动Apache和Mysql
先用chkconfig --list查询apache和mysql服务是否存在,不存在则需要手动添加 [root@centos64 vsftpd]# chkconfig --list 测试存在,只需要开 ...
- CentOS7安装MySQL8.0小计
之前讲配置文件和权限的时候有很多MySQL8的知识,有同志说安装不太一样,希望发个文,我这边简单演示一下 1.环境安装 下载MySQL提供的CentOS7的yum源 官方文档:<https:// ...