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. android实现文本复制到剪切板功能(ClipboardManager)

    Android也有剪切板(ClipboardManager),可以复制一些有用的文本到剪贴板,以便用户可以粘贴的地方使用,下面是使用方法   注意:导包的时候 API 11之前: android.te ...

  2. js解析json,js转换json成map,获取map的key,value

    json串格式 { "10.10.11.1": { "target_1": "34.2", "target_3": &q ...

  3. SD卡中FAT32文件格式高速入门(图文具体介绍)

    说明: MBR :Master Boot Record ( 主引导记录) DBR :DOS Boot Record ( 引导扇区) FAT :File Allocation Table ( 文件分配表 ...

  4. java.lang.OutOfMemoryError: Java heap space错误及处理办法(收集整理、转)

    下面是从网上找到的关于堆空间溢出的错误解决的方法: java.lang.OutOfMemoryError: Java heap space ============================== ...

  5. Jenkins - 持续集成

    Jenkins是一个开源软件项目,旨在提供一个开放易用的软件平台,使持续集成变成可能. Jenkins,之前叫做Hudson,是基于Java开发的一种持续集成工具,用于监控秩序重复的工作,包括: 1. ...

  6. 世界最大射电望远镜(Arecibo)用于探測地外文明

    请看下图: 这是眼下世界最大口径(305米)射电望远镜的外观图,位于美国中西部的PuertoRico州,建成于1963年,工作波段在3厘米至1米波段范围内,正好适合用于探測地外文明(SETI)的科学研 ...

  7. 使用断言assert

    之前有看过关于Assert的书,但是不懂得如何去用,最近看别人写的代码有用这个断言(assert),今天自己动手看看如何使用断言. 断言(assert)的语义如下:如果表达式的值为0(假),则输出错误 ...

  8. 作业三 ATM

    模拟实现一个ATM+购物商场程序 1.额度15000自定义 商城和银行两个帐户 2.实现购物商城,买东西加入购物车,调用信用卡接口结账 3.可以提现,手续费5%,提现额度不能超过50% 4.每月22日 ...

  9. 图片设置3D效果

    /** * 图片绘制3d效果 * @param srcImage * @param radius * @param border * @param padding * @return * @throw ...

  10. <display:table>属性解释

    参考官方网站:http://www.displaytag.org/1.2/displaytag/tagreference.html 所有属性: cellpadding,cellspacing,clas ...