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+ ...
随机推荐
- AJAX是什么?
AJAX的全称是Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). ajax不是新的编程语言,而是一种使用现有标准的新方法.ajax是与服务器 ...
- java并发--流量控制demo
实现一个流控程序.控制客户端每秒调用某个远程服务不超过N次,客户端是会多线程并发调用,需要一个轻量简洁的实现,大家看看下面的一个实现,然后可以自己写一个实现. import java.util.Dat ...
- ICE中间件相关
Ice 是 网络通信引擎 Internet Communications Engine 的简称,是ZeroC开发的一个面向对象的中间件平台.它提供了面向对象的远程过程调用.网格计算和发布/订阅功能,并 ...
- 【Java】JavaIO(二)、节点流
一.InputStream & outputStream Java字节流主要是以InputStream (输入流),outputStream(输出流)为基类,本身是抽象类不能创建实例,但是是字 ...
- HTML5 画图--文字
1:html <div style="margin:0 auto;width:794px;height:1123px"> <canvas id="myC ...
- datagrid数据表格的维护
想想刚开始学jsp, 用application做一个简单的数据库, 简单的注册页面, 跟这个相比就是过家家 <%@ page language="java" contentT ...
- 87. Scramble String (String; DP)
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- Treasures and Vikings(两次搜索)
Treasures and Vikings https://www.luogu.org/problemnew/show/P4668 题意翻译 你有一张藏宝图,藏宝图可视为 N×MN×M 的网格.每个格 ...
- 【Python基础教程第2版】——第一讲:基础知识
1.长字符串:(用三引号如'''或者"""来引起来) >>> print """This is a very log st ...
- 本地Maven环境配置
本地Maven环境配置 下载配置文件:http://10.1.10.138:6060/root/DevelopmentSpecification/archive/master.zip 解压master ...