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. 解析Xcode把应用程序打包成ipa---解决打包完新版本itunes提示不是有效应用程序的问题

    Xcode把应用程序打包成ipa是本文要介绍的内容,不多说,先俩看内容.注意:本方法需要先制作假凭证编译于项目中,否则产生的ipa还是无法于iPhone中运行. 制作方法请参考: http://blo ...

  2. [置顶] .net技术类面试、笔试题汇总3

    今天本人从成都回到了学校,深刻认识了自己存在很多不足,在这段期间会更加努力,争取早日找到一个好工作! 41.在ASP.NET中有Button控件myButton,要是单击控件时,导航到其他页面http ...

  3. com.microsoft.sqlserver.jdbc.SQLServerException: 结果集没有当前行

    參考博客com.microsoft.sqlserver.jdbc.SQLServerException: 结果集没有当前行 java获取结果集,if(rs!=null).和while(rs.next( ...

  4. 升级Android ADT 和SDK

    因为眼下从事android开发工作,所以升级了下Android SDK和eclipse ADT插件 一.更新ADT 1.Eclipse中打开Help->Install New Software. ...

  5. 如何判断手机收有几张SIM卡

    //判断卡槽1中是不是有卡 boolean hasIccCard1 = MSimTelephonyManager.getDefault().hasIccCard(0); //判断卡槽2中是不是有卡 b ...

  6. android中定位光标位置

    edittext.setSelection(int); edittext.setText(123);//设置edittext中的内容 edittext.setSelection(123.length( ...

  7. 解决SQL Server 占用80端口

    停用掉下面的服务就可以了:

  8. NHIBERNATE之映射文件配置说明(转载4)

    二十.自定义值类型   开发者创建属于他们自己的值类型也是很容易的.比如说,你可能希望持久化Int64类型的属性, 持久化成为VARCHAR 字段.NHibernate没有内置这样一种类型.自定义类型 ...

  9. java 位运算权限管控(转载)

    这里笔者介绍一种很常用,也比较专业的权限控制思路.这里用java语言描述,其实都差不多的.要换成其他的语言主,自己转一下就可以了.为了方便起见,我们这里定义a^b为:a的b次方.这里,我们为每一个操作 ...

  10. taglib的使用

    使用自定义的taglib可以是我们对页面数据的处理放在后台,不仅使用方便,而且影藏了处理逻辑,也更加的安全. 需要使用到servlet.jar 1.在web-inf下建立taglib.tld文件 &l ...