poj 2376 Cleaning Shifts(贪心)
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
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(贪心)的更多相关文章
- POJ 2376 Cleaning Shifts 贪心
Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...
- POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)
Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...
- poj 2376 Cleaning Shifts 贪心 区间问题
<pre name="code" class="html"> Cleaning Shifts Time Limit: 1000MS Memory ...
- POJ 2376 Cleaning Shifts (贪心,区间覆盖)
题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小 ...
- 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【贪心】
POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...
- 【原创】poj ----- 2376 Cleaning Shifts 解题报告
题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K ...
- poj 2376 Cleaning Shifts 最小区间覆盖
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40751 Accepted: 9871 ...
- ACM学习历程——POJ 2376 Cleaning Shifts(贪心)
Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning ...
随机推荐
- 【Python爬虫基础】抓取知乎页面所有图片
抓取地址所有图片 #! /usr/bin/env python from urlparse import urlsplit from os.path import basename import ur ...
- iOS 捕获系统外异常
iOS 捕获系统外异常 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太 ...
- [React Testing] Redux Reducers
Sometimes we want to test our Redux reducers to make sure they work as expected. In this lesson we w ...
- HBase学习(十四)LINUX下用Eclipse构建HBase开发环境
Eclipse,HBase版本号眼下没有发现须要特别指定 1:从HBase集群中复制一份Hbase部署文件,放置在开发端某一文件夹下(如在/app/hadoop/hbase096文件夹下). 2:在e ...
- hexo博客的优化与配置——加入统计代码
今天看着csdn博客的訪客数,就想给hexo博客也加入统计訪客的插件,上次折腾了个pacman主题,中间自带的是goole的统计,easy被墙,所以就想换一个统计工具,看了好多人用的都是cnzz的站长 ...
- KindEditor简单的Demo使用
一般的做网站后台都会用到富文本编辑器,网上也有很多优秀的富文本编辑器,这里是开源中国的富文本编辑器推荐:http://www.oschina.net/project/tag/172/wysiwyg 我 ...
- 对return 语句的正确性和效率进行检查
注意事项如下: 1. return 语句不可返回指向"堆栈内存“的”指针“或者”引用“,因为该内存单元在函数体结束时被自动释放. //错误 char* Func(void) { char s ...
- USB通讯协议之深入理解
0. 基本概念 一个[传输](控制.批量.中断.等时):由多个[事务]组成: 一个[事务](IN.OUT.SETUP):由一多个[Packet]组成. USB数据在[主机软件]与[USB设备特定的端点 ...
- Web应用程序项目XXXX已配置为使用IIS。无法访问IIS元数据库。您没有足够的特权访问计算机上的IIS网站
问题:Windows8下直接使用VS打开项目,出现问题:XXXX已配置为使用IIS.无法访问IIS元数据库.您没有足够的特权访问计算机上的IIS网站.解决:1.以“管理员权限”运行VS,在VS菜单打开 ...
- Task与Thread间的区别
通过查找一些文章,得知,Task与Thread不可比.Task是为了利用多CPU多核的机制而将一个大任务不断分解成小任务,这些任务具体由哪一个线程或当前线程执行由OS来决定.如果你想自己控制由哪一个T ...