Cleaning Shifts
Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T.

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

* Line 1: Two space-separated integers: N and T

* 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

* Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.

Sample Input

3 10
1 7
3 6
6 10

Sample Output

2

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

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.

 
 
题意:农夫John和他的cows系列。这次cows们被John命令cleaning shifts,给你总区间时间,和每头cow负责clean的子区间时段,问怎样安排可以使最少的cow打扫于整个时间区间。
思路:典型贪心。先对所有cow开始clean的时间升序排序,然后再依次遍历每头cow,保证开始时间在上一头cow时段内,使结束时间最长。(注意:不重叠但相邻也成立)一旦超出上一头cow的结束时段,更新cow,并继续向下遍历。
 
 
#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 贪心(最小区间覆盖)的更多相关文章

  1. POJ 2376 Cleaning Shifts (贪心,区间覆盖)

    题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小 ...

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

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

  3. POJ 2376 Cleaning Shifts 贪心

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

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

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

  5. poj 2376 Cleaning Shifts

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

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

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

  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. Android 短信验证码控件

    Android 短信验证码控件,便于项目中使用统一样式,统一提示改动.个人觉得挺好用的 <span style="font-size:18px;">public cla ...

  2. 流迭代器 + 算法灵活控制IO流

    前言 标准算法配合迭代器使用太美妙了,使我们对容器(数据)的处理更加得心应手.那么,能不能对IO流也使用标准算法呢?有人认为不能,他们说因为IO流不是容器,没有迭代器,故无法使用标准算法.他们错了,错 ...

  3. 如何获取ipa 包的图片

    突然想起当初刚学习iOS的时候,就经常通过抓包和提取素材的方式来模仿App,今天就教大家如何一步步提取App的素材! 大家是否有过想要获取别人的素材的想法?看到某些App的资源很不错,很想导出来用用, ...

  4. php 中 isset 和empty 的区别

    昨天终于开始学习php了,这个对于我来说听着很熟悉,但是学起来很陌生的东西,尤其是课上能听明白 但是真正写起了手生,都不知道手该往哪里放了,天哪~~~ 其中课上有讲到 isset和empty的区别,现 ...

  5. wepy开发

    工欲善其事必先利其器 ide安装.配置] https://tencent.github.io/wepy/document.html VS Code   1. 在 Code 里先安装 Vue 的语法高亮 ...

  6. flume-ng script should first try finding java from PATH and then try using bigtop, instead of vice-versa

    [FLUME-1154] flume-ng script should first try finding java from PATH and then try using bigtop, inst ...

  7. pdf文件的作成

    Dim Report As New crProgressList Report.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.P ...

  8. appium(11)-java-client

    Welcome to the Appium Java client wiki! This framework is an extension of the Selenium Java client. ...

  9. Hihocoder #1095 : HIHO Drinking Game (微软苏州校招笔试)( *【二分搜索最优解】)

    #1095 : HIHO Drinking Game 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi and Little Ho are playin ...

  10. .cxx_destruct crash

    开发过程中遇到 YXTBaseLabelCell .cxx_destruct崩溃,查了下,会在调用类的dealloc方法时调用cxx_destruct,于是看了下代码,找dealloc可能会崩溃的原因 ...