POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)
Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval.
Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.
Input
* Lines 2..N+1: Each line contains the start and end times of the interval during which a cow can work. A cow starts work at the start time and finishes after the end time.
Output
Sample Input
3 10
1 7
3 6
6 10
Sample Output
2
Hint
INPUT DETAILS:
There are 3 cows and 10 shifts. Cow #1 can work shifts 1..7, cow #2 can work shifts 3..6, and cow #3 can work shifts 6..10.
OUTPUT DETAILS:
By selecting cows #1 and #3, all shifts are covered. There is no way to cover all the shifts using fewer than 2 cows.
#include<stdio.h>
#include<algorithm>
using namespace std; struct Node{
int l,r;
}node[];
bool cmp(Node a,Node b)
{
if(a.l==b.l) return a.r>b.r;
return a.l<b.l;
}
int main()
{
int n,t,c,f,min,max,i;
scanf("%d%d",&n,&t);
for(i=;i<=n;i++){
scanf("%d%d",&node[i].l,&node[i].r);
}
sort(node+,node+n+,cmp);
c=;f=;
min=node[].r;
max=node[].r;
for(i=;i<=n;i++){
if(node[i].l<=min+){ //坑点。。相邻即相连
if(node[i].r>max){
f=;
max=node[i].r;
if(max>=t) break;
}
}
else{
min=max;
c++;
f=;
if(node[i].l<=min+){ //同上
if(node[i].r>max){
f=;
max=node[i].r;
if(max>=t) break;
}
}
else break;
}
}
if(node[].l<=&&max>=t) printf("%d\n",c+f);
else printf("-1\n");
return ;
}
POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)的更多相关文章
- POJ 2376 Cleaning Shifts (贪心,区间覆盖)
题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小 ...
- poj 2376 Cleaning Shifts 贪心 区间问题
<pre name="code" class="html"> Cleaning Shifts Time Limit: 1000MS Memory ...
- 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 最小区间覆盖
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40751 Accepted: 9871 ...
- 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+ ...
随机推荐
- Android性能优化之中的一个 布局优化
本文为Android性能优化--布局优化,主要介绍使用抽象布局标签(include, viewstub, merge).去除不必要的嵌套和View节点.降低不必要的infalte及其它Layout方面 ...
- 用callgraph生成的两张函数调用关系图
参考这里,感觉很Cool吧. Linux-0.11函数调用关系图: QEMU函数调用关系图:
- iOS 移动开发周报
iOS 移动开发周报 前言 是的,我又开始写周报了!主要是因为喵神不写周报了,加上我发现大家对写 iOS 技术周报这件事情似乎没什么兴趣.其实我觉得这是一个挺好的学习总结的办法,所以要不就继续我来 ...
- C语言宏定义时#(井号)和##(双井号)作用
#的功能是将其后面的宏参数进行字符串化操作(Stringfication),简单说就是在对它所引用的宏变量 通过替换后在其左右各加上一个双引号. #define example(instr) prin ...
- 代写GIS系统
代写GIS系统,熟悉arcgis,leaflet ,百度地图等api.可以提供系统代写,技术咨询等
- 2016/07/07 apmserv5.2.6 Apache启动失败,请检查相关配置。MySQL5.1已启动。
因为要用PHP做一个程序,在本机上配PHP环境,下了个APMServ5.26,安装很简单,不再多说,装好后,启动,提示错误,具体是:“Apache启动失败,请检查相关配置.√MySQL5.1已启动”, ...
- SAM4E单片机之旅——1、LED闪烁之空循环
最近因为导师要写一本关于SAME4单片机的书籍,而我也作为一个嵌入式的初学者看了这本书.现在也让我写写几个小的程序,做做示例.既然写了文档之类的,就发到博客上来吧. 目前关于这芯片能参考的书籍大概就只 ...
- 用EasyClient开源项目采集Windows摄像头/麦克风的音视频进行RTSP直播
EasyClient是EasyDarwin开源流媒体团队开发的一款功能丰富的开源PC客户端项目,目前支持Windows.Android版本,后续将支持ios版本,其中Windows版本的EasyCli ...
- 2018.11.06 生成器函数进阶&列表推导式&生成器表达式
1.生成器函数进阶 2.列表推导式 3.生成器表达式
- 第一节 麒麟系统安装+基础环境搭建(JDK+Scala)
本文重点对没有Linux基础的人员提供高速上手的指导,假设你的开发环境已经搭建好,能够略过本章所讲内容,内容来源于网络.也谢谢这些默默讲自己经验分享的人!近期在学习大数据,有喜欢的朋友能够一起研究. ...