题意:给定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. php, postgresql 安装

    sudo yum install postgresql84-server postgresql84-contrib ubuntu下面安装的问题解决: Postgresql installation o ...

  2. salt之grains组件

    grains是saltstack最重要的组件之一,作用是收集被控主机的基本信息,这些信息通常都是一些静态类的数据,包括CPU.内核.操作系统.虚拟化等,在服务器端可以根据这些信息进行灵活定制,管理员可 ...

  3. How to Pronounce the I in ING

    How to Pronounce the I in ING Share Tweet Share Tagged With: ING Verbs The I in ING is the IH as in ...

  4. cocos2d-x 3.0 学习笔记: 一个可以拖拽的Label及schedule的应用

    #ifndef _DRAGLABEL_H_ #define _DRAGLABEL_H_ #include "cocos2d.h" USING_NS_CC; class DragLa ...

  5. 是否需要主动调用Bitmap的recycle方法

    一个Bitmap使用完后,是只需要等它成为垃圾后让GC去回收,还是应该主动调用recycle方法呢?或者说,主动调用recycle方法是否有好处,是否能马上回收内存呢? 带着这个问题来看源码(我看的4 ...

  6. fm 讲解加代码

    转自: 博客 http://blog.csdn.net/google19890102/article/details/45532745/ github https://github.com/zhaoz ...

  7. Go语言中cannot convert adminname (type interface {}) to type *: need type assertion的解决办法

    解决的办法是把string(adminname)替换为adminname.(string).其它类型也是类似.

  8. 获取RequestMapping注解中的属性

    参考:https://www.cnblogs.com/2013jiutian/p/7294053.html @RequestMapping("/value1") @Controll ...

  9. Matlab中插值函数汇总(上)

    Matlab中插值函数汇总分上下两个部分,主要整合自matlabsky论坛dynamic发表于2009-2-21 21:53:26 的主题帖,以及豆丁网rickoon上传的教材第8章<插值,拟合 ...

  10. 2.Add Two Numbers (List)

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...