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 ...
随机推荐
- 【C#通用类】日志记录类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- VS2010旗舰版安装图解
微软公布了最新的 Visual Studio 2010 软件开发编程平台及 .Net Framework 4 框架.这次 VisualStudio 2010 包含 Professional 专业版.P ...
- Jenkins - 持续集成
Jenkins是一个开源软件项目,旨在提供一个开放易用的软件平台,使持续集成变成可能. Jenkins,之前叫做Hudson,是基于Java开发的一种持续集成工具,用于监控秩序重复的工作,包括: 1. ...
- repo简介
高通的Android JellyBean库已经建立完成,目前已经可以正常访问,可以正常提交.Repo获取代码注意:1. 首先应有一个repo脚本,并将脚本放到环境变量里,此脚本的配置方法a) 进入用户 ...
- [Linux 小技巧] Ubuntu 14.04 下编译、安装、配置最新开发版 GoldenDict
1. 背景介绍 GoldenDict 是一款非常优秀的跨平台电子词典软件,支持 StarDict.Babylon 等多种词典.其 PC 版基于 Qt 编写,完全免费.开源且没有广告.GoldenDic ...
- 使用const 提高函数的健壮性
1.如果输入参数采用“指针传递”,那么加const 修饰可以防止意外地改动该指针指向的内存单元,起到保护的作用. 例如:void StringCopy(char *strDest, const cha ...
- python模块基础之OS模块
OS模块简单的来说它是一个Python的系统编程的操作模块,可以处理文件和目录这些我们日常手动需要做的操作. 可以查看OS模块的帮助文档: >>> import os #导入os模块 ...
- ansible 学习与实践
title: ansible 学习与实践 date: 2016-05-06 16:17:28 tags: --- ansible 学习与实践 一 介绍 ansible是新出现的运维工具是基于Pytho ...
- html 文字溢出标签
overflow:visible;作用:能看到溢出部分. overflow: hidden;作用:溢出部分看不到 overflow:scroll; 作用:出现一个滚动条(不超过的文字也会在滚动条里) ...
- DOM中的NodeList与HTMLCollection
最近在看<Javascript高级程序设计>的时候,看到了这样一句话:“理解NodeList和HTMLCollection,是从整体上透彻理解DOM的关键所在.”,所以觉得应该写一篇关于N ...