POJ 2376 Cleaning Shifts (贪心,区间覆盖)
题意:给定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 (贪心,区间覆盖)的更多相关文章
- poj 2376 Cleaning Shifts 贪心 区间问题
<pre name="code" class="html"> Cleaning Shifts Time Limit: 1000MS Memory ...
- poj 2376 Cleaning Shifts 最小区间覆盖
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40751 Accepted: 9871 ...
- POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)
Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...
- POJ 2376 Cleaning Shifts 贪心
Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...
- POJ 2376 Cleaning Shifts(轮班打扫)
POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Farmer ...
- poj 2376 Cleaning Shifts
http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 2376 Cleaning Shifts 区间覆盖问题
http://poj.org/problem?id=2376 题目大意: 给你一些区间的起点和终点,让你用最小的区间覆盖一个大的区间. 思路: 贪心,按区间的起点找满足条件的并且终点尽量大的. 一开始 ...
- poj 2376 Cleaning Shifts(贪心)
Description Farmer John <= N <= ,) cows to <= T <= ,,), the first being shift and the la ...
- POJ 2376 Cleaning Shifts【贪心】
POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...
随机推荐
- BLOB 操作
对于数据库是BLOB类型存储数据. BLOB数据插入: Oracle提供的标准方式: 先插入一个空BLOB对象,然后Update这个空对象. 首先使用empty_blob()函数插入一个空BLOB对象 ...
- linux no space left on device
一般有两个原因: 1.磁盘空间不足 2.inode不足 用df -h查看磁盘空间使用情况 用df -i查看inode使用情况
- Oracle创建表语句(Create table)语法详解及示例
创建表(Create table)语法详解1. ORACLE常用的字段类型ORACLE常用的字段类型有VARCHAR2 (size) 可变长度的字符串, 必须规定长度CHAR(size) 固定长度的字 ...
- Scala语言学习笔记(2)
表达式,值,变量,代码块,函数,方法 // 表达式 1 + 1 println(1 + 1) // 2 // 值(values)使用 val 关键字声明,带初值时类型可省略. val x = 1 + ...
- Delphi 动态数组合并
TIntArray = array of Integer; function MergeArray(const ArrayA, ArrayB: TIntArray): TIntArray; var i ...
- 遇到的IE不兼容问题总结
IE浏览器兼容问题困扰多时,由于IE6不在进行修补,IE6的考虑也越来越少,有些IE遇到的不兼容的现象想做一个总结 1宽度或高度:IE的是width+border+margin+padding goo ...
- 关于struts.xml配置文件的说明
<struts> <!-- action: 对应controller 中的类的 name: 匹配url要访问的类 class:包名+类名 通过反射产生对象 method:指定默认访问 ...
- php获取微信用户信息(没测试过)
<?php /** * 通过$appid.$appsecret获得基础支持的接口唯一凭证access_token,返回值为array类型 */ function get_access_token ...
- javascritp伪协议
[javascritp伪协议] 将javascript代码添加到客户端的方法是把它放置在伪协议说明符javascript:后的URL中.这个特殊的协议类型声明了URL的主体是任意的javascript ...
- mydqldump
[mydqldump] One way to create a snapshot of the data in an existing master database is to use the my ...