题目链接: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:贪心的更多相关文章

  1. 3433: [Usaco2014 Jan]Recording the Moolympics

    3433: [Usaco2014 Jan]Recording the Moolympics Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 137  S ...

  2. 【BZOJ】3433: [Usaco2014 Jan]Recording the Moolympics (贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3433 想了好久啊....... 想不出dp啊......sad 后来看到一英文题解......... ...

  3. BZOJ3433: [Usaco2014 Jan]Recording the Moolympics

    3433: [Usaco2014 Jan]Recording the Moolympics Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 55  So ...

  4. 【bzoj 3433】{Usaco2014 Jan} Recording the Moolympics(算法效率--贪心)

    题意:给出n个区间[a,b),有2个记录器,每个记录器中存放的区间不能重叠.求2个记录器中最多可放多少个区间. 解法:贪心.只有1个记录器的做法详见--关于贪心算法的经典问题(算法效率 or 动态规划 ...

  5. BZOJ 3430: [Usaco2014 Jan]Ski Course Rating(并查集+贪心)

    题面 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 136 Solved: 90 [Submit][Status][Discuss] Descript ...

  6. bzoj 1783: [Usaco2010 Jan]Taking Turns【贪心+dp】

    不知道该叫贪心还是dp 倒着来,记f[0][i],f[1][i]分别为先手和后手从n走到i的最大值.先手显然是取最大的,当后手取到比先手大的时候就交换 #include<iostream> ...

  7. BZOJ 3432: [Usaco2014 Jan]Cross Country Skiing (二分+染色法)

    还是搜索~~可以看出随着D值的增大能到达的点越多,就2分d值+染色法遍历就行啦~~~ CODE: #include<cstdio>#include<iostream>#incl ...

  8. [bzoj 3048] [Usaco2013 Jan]Cow Lineup

    [bzoj 3048] [Usaco2013 Jan]Cow Lineup Description 给你一个长度为n(1<=n<=100,000)的自然数数列,其中每一个数都小于等于10亿 ...

  9. BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花( 贪心 )

    考虑相邻的两头奶牛 a , b , 我们发现它们顺序交换并不会影响到其他的 , 所以我们可以直接按照这个进行排序 ------------------------------------------- ...

随机推荐

  1. PHP -- 问题

    @.源码安装,最开始,自己通过 ./config  make  make install三步,啥参数也没加,安装好之后,发现/usr/local/php下就一个man文件夹,死都没找到php-fpm之 ...

  2. PYTHON流向下载

    #-*- coding:utf-8 -*- import gzip import re import http.cookiejar import urllib.request import urlli ...

  3. DJI SDK iOS 开发之中的一个:前言

    写这个开发教程之前,还是先说点什么. 首先要声明的是我并非DJI的员工.仅仅是DJI 飞行器的爱好者. 在DJI的phantom出来之后.我就一直期待着能够推出SDK.之前最早是Parrot的AR D ...

  4. Cobbler部署之FAQ处理

    Cobbler报错处理 通过cobbler check检查出现的报错 红色标注为报错关键信息 9.1 报错一 # cobbler check httpd does not appear to be r ...

  5. vue实践---vue不依赖外部资源实现简单多语

    vue使用多语,最常见的就是 vue-i18n, 但是如果开发中的多语很少,比如就不到10个多语,这样就没必要引入vue-i18n了, 引入了反正导致代码体积大了,这时候单纯用vue实现多语就是比较好 ...

  6. window 怎么样让nginx开机自启动

    安装Nginx 下载windows版nginx (http://nginx.org/download/nginx-1.10.0.zip),之后解压到需要放置的位置(D:\xampp\nginx) 将N ...

  7. 第三课 nodejs读取文件

    //引入文件操作模块var fs = require('fs'); //读取文件 使用 回调函数 utf-8编码读取 a.txt在当前文件目录fs.readFile('a.txt','UTF-8',f ...

  8. java List的相关工具类

    1. <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</ar ...

  9. Zookeeper Curator 事件监听 - 秒懂

    目录 写在前面 1.1. Curator 事件监听 1.1.1. Watcher 标准的事件处理器 1.1.2. NodeCache 节点缓存的监听 1.1.3. PathChildrenCache ...

  10. L2 范数 L1 范数 出租车范数

    https://en.wikipedia.org/wiki/Norm_(mathematics) http://cs231n.github.io/classification/