题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1.

析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后,

忽略小于1的部分(如果有的话),再找最长的区间,然后把这个区间的右端点作为下次寻找的起点,

再找最大区间,直到覆盖到最后。

注意:首先要判断好能不能覆盖,不能覆盖就结束,有可能会提前结束,也要做好判断,我就在这WA了好几次,

悲剧。。。其他的就比较简单了,不用说了。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm> using namespace std;
const int maxn = 25000 + 10;
struct node{
int l, r;
bool operator < (const node &p) const {
return l < p.l;
}
};
node a[maxn]; int main(){
int n, m;
scanf("%d %d", &n, &m);
for(int i = 0; i < n; ++i) scanf("%d %d", &a[i].l, &a[i].r);
sort(a, a+n); bool ok = true;
int s = 1, e = 1, cnt = 1;
if(a[0].l > 1){ printf("-1\n"); return 0; } for(int i = 0; i < n; ++i){
if(a[i].l <= s) e = max(e, a[i].r);
else{
++cnt;
s = e + 1;
if(a[i].l > s){ ok = false; break; }
else e = max(e, a[i].r);
}
if(e >= m) break;
} if(!ok || e < m) printf("-1\n");
else printf("%d\n", cnt);
return 0;
}

POJ 2376 Cleaning Shifts (贪心,区间覆盖)的更多相关文章

  1. poj 2376 Cleaning Shifts 贪心 区间问题

    <pre name="code" class="html"> Cleaning Shifts Time Limit: 1000MS   Memory ...

  2. poj 2376 Cleaning Shifts 最小区间覆盖

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40751   Accepted: 9871 ...

  3. POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)

    Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...

  4. POJ 2376 Cleaning Shifts 贪心

    Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...

  5. POJ 2376 Cleaning Shifts(轮班打扫)

    POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Farmer ...

  6. poj 2376 Cleaning Shifts

    http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  7. POJ 2376 Cleaning Shifts 区间覆盖问题

    http://poj.org/problem?id=2376 题目大意: 给你一些区间的起点和终点,让你用最小的区间覆盖一个大的区间. 思路: 贪心,按区间的起点找满足条件的并且终点尽量大的. 一开始 ...

  8. poj 2376 Cleaning Shifts(贪心)

    Description Farmer John <= N <= ,) cows to <= T <= ,,), the first being shift and the la ...

  9. POJ 2376 Cleaning Shifts【贪心】

    POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...

随机推荐

  1. Ambari安装Hadoop集群

    * System Environment:centOS6.7 1.Prepare the Environment 1)Set Up Password-less SSH : (Generate publ ...

  2. 5.15 python 面向对象的软件开发&领域模型

    1.面向对象的软件开发 参考地址::http://www.cnblogs.com/linhaifeng/articles/6182264.html#_label14 面向对象的软件工程包括下面几个部: ...

  3. 转。。原理同样支持 delphi

    我用C#导出excel 带图片,用office2003 正常,但换成office 2007 时,我指定多个单元格分别插入图片,这个图片不在此单元格内,这些图片全都集中在一起,在一个位置上.很奇怪,看起 ...

  4. time和datetime

    一.time模块常用函数1. time()函数time()函数返回的是时间戳(timestamp).所谓时间戳指的是从1970年1月1日00:00:00开始按秒计算的偏移量.其他返回时间戳方式的函数还 ...

  5. 奇偶数判断2(if else+switch语句)

    public class 奇偶数判断2 { public static void main(String [] agrs){ float s = 17f; //定义浮点型数据s float h = s ...

  6. Windows phone 自定义控件(无外观控件)——ColorPicker

    编码前 在上一篇博客中,写的是一个UserControl的子类,它具有固定的外观(虽然也可以通过样式来进行修改,但受到的限制很大).如果你想要使用这个控件的逻辑,但是希望在使用的时候可以更改控件的外观 ...

  7. Codeforces Round #534 (Div. 2)

    B. Game with string 题意: 给出一个字符串s只包括小写字母.当轮到一个玩家的时候,他可以选择两个连续且相等的字母并且删除它.当一个玩家没得删的时候他就输了. 题解: 乍一看有点懵, ...

  8. jquery 动态添加的代码不能触发绑定事件

    今天发现jQuery对动态添加的元素不触发事件,比如blur.click事件等 参考文章证明了我的结论,并给出了原因及解决方案 原因:程序找不到动态添加的节点. 解决方案:在绑定父元素后的子元素 $( ...

  9. OpenStack的架构详解(转)

    OpenStack既是一个社区,也是一个项目和一个开源软件,它提供了一个部署云的操作平台或工具集.其宗旨在于,帮助组织运行为虚拟计算或存储服务的云,为公有云.私有云,也为大云.小云提供可扩展的.灵活的 ...

  10. CentOS 7安装Samba 4.6 版本步骤及错误解决方法

    首先通过这次教训,让我养成一个好习惯:备份  备份  备份      不管做什么配置或者更改什么东西之前先做好备份! 还有我本身的一个坏毛病:眼高手低! 工厂有一台服务器,由以前的运维装的Samba ...