Description

Farmer John is assigning some of his N ( <= N <= ,) 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 ( <= T <= ,,), the first being shift  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 -.

Input

* Line : Two space-separated integers: N and T
* Lines ..N+: 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 : The minimum number of cows Farmer John needs to hire or - if it is not possible to assign a cow to each shift.

Sample Input


Sample Output


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.

Source

 

题目:

给定一个时间T和N个时间区间,求最少需要多少个区间覆盖总区间[1,T],无法覆盖区域[1,T]时输出-1。

例如T=10,有3个区间[1,7],[3,6],[8,10],则最少需要两个区间来覆盖,选择区间1和区间3。

解题思路:

使用贪心法。首先将区间按开始时间从小到大排序,开始时间相等按结束时间从小到大排序。

1 如果第一个区间不是从1开始,则无法覆盖,输出-1。

2 令当前覆盖到的时间time为开始时间为1的区间中结束时间的最大值。

3 从开始时间不为1的区间开始循环遍历。

4 选择合法区间中结束时间值最大的那个区间,合并到[1,time],合并后time为选择的区间的结束时间。所谓合法区间是指区间的起始时间start<=time+1,这样才能和区间[1,time]合并。如果没有找到合法区间或者找到的区间结束时间比time小,则无法覆盖,结束循环。

5 循环结束后,根据time是否大于等于T,输出结果。

 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define N 26000
struct Node
{
int st,ed;
}p[N];
bool cmp(Node a,Node b){
if(a.st!=b.st)
return a.st<b.st;
return a.ed<b.ed;
}
int main()
{
int n,t;
while(scanf("%d%d",&n,&t)==)
{
for(int i=;i<n;i++)
{
scanf("%d%d",&p[i].st,&p[i].ed);
} sort(p,p+n,cmp); int i=;
int time=p[i].ed;
if(p[i].st!=)
{
printf("-1\n");
continue;
}
i++;
while(i<n && p[i].st==)
{
time=p[i].ed;
i++;
}
int ans=; while(time<t)
{ if(i>=n)
break;
int tmp=i;
int w=p[i].ed;
i++;
while(i<n && p[i].st<=time+)
{
if(w<p[i].ed)
{
tmp=i;
w=p[i].ed;
}
i++;
} if(w<=time || p[tmp].st>time+)
{
break;
}
else
{
time=w;
ans++;
} }
if(time<t)
ans=-; printf("%d\n",ans);
}
return ;
}

poj 2376 Cleaning Shifts(贪心)的更多相关文章

  1. POJ 2376 Cleaning Shifts 贪心

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

  2. POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)

    Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...

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

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

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

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

  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【贪心】

    POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...

  8. 【原创】poj ----- 2376 Cleaning Shifts 解题报告

    题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K ...

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

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

  10. ACM学习历程——POJ 2376 Cleaning Shifts(贪心)

    Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning ...

随机推荐

  1. eclipse format的时候如何让@param后不换行

  2. Quartz中时间表达式的设置-----corn表达式

    Quartz中时间表达式的设置-----corn表达式 时间格式: <!-- s m h d m w(?) y(?) -->,   分别相应: 秒>分>小时>日>月 ...

  3. 【转】MVP和MVC的区别

    转自:http://www.cnblogs.com/end/archive/2011/06/02/2068512.html MVC和MVP到底有什么区别呢? 从这幅图可以看到,我们可以看到在MVC里, ...

  4. Selenium2学习之-环境搭建

    1.下载安装Eclipse 2.下载并配置jdk环境变量 2.1 介绍一下环境变量设置 2.1.1 点击环境变量 2.1.2 新增系统变量JAVA_HOME 变量名:JAVA_HOME 变量值:C:\ ...

  5. OD: Heap Exploit : DWORD Shooting & Opcode Injecting

    堆块分配时的任意地址写入攻击原理 堆管理系统的三类操作:分配.释放.合并,归根到底都是对堆块链表的修改.如果能伪造链表结点的指针,那么在链表装卸的过程中就有可能获得读写内存的机会.堆溢出利用的精髓就是 ...

  6. JavaScript绑定事件的方法[3种]

    在JavaScript中,有三种常用的绑定事件的方法: 在DOM元素中直接绑定: 在JavaScript代码中绑定: 绑定事件监听函数. 一. 在DOM元素中直接绑定 这里的DOM元素,可以理解为HT ...

  7. hdu 2013

    水题 AC代码: #include <iostream> using namespace std; int main() { int i,m,n; while(cin>>n) ...

  8. 用ueditor上传图片、文件等到七牛云存储

    ueditor上传文件,是用数据流的形式上传的. 而七牛云存储官方文档中,只提供了文件路径上传的方式. 但是,仅仅是在官方文档中写了这一种方式. 事实上,利用VS的对象管理器,打开Qiniu的dll, ...

  9. (转)使用Microsoft Web Application Stress Tool对web进行压力测试

    http://www.blogjava.net/crespochen/archive/2009/06/02/279538.html Web压力测试是目前比较流行的话题,利用Web压力测试可以有效地测试 ...

  10. Android -------- 网络访问数据