POJ:2367-Cleaning Shifts
传送门:http://poj.org/problem?id=2376
Cleaning Shifts
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 27272 Accepted: 6724
Description
Farmer John is assigning some of his N (1 <= N <= 25,000) 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 (1 <= T <= 1,000,000), the first being shift 1 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 -1.
Input
Line 1: Two space-separated integers: N and T
Lines 2..N+1: 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 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.
Sample Input
3 10
1 7
3 6
6 10
Sample Output
2
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.
解题心得:
- 题意就是让你选择的最少的线段将整个区间完全覆盖,如果不能完全覆盖输出-1。
- 可以使用贪心的思想,首先按照起始时间排序,如果起始时间相同,让结束时间越长的排在越前面,每次找一个线段,然后找起点在线段内,但是终点要更远的线段作为新的终点,维护远的终点,如果起点超出了当前选择的线段答案就+1。
- 然后就是判断是否出现无线段覆盖的地方,这个就需要判断答案+1的地方和开始选择的第一个线段是否是从1开始的,主要是注意一下边界问题。
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 25000;
int n,t;
struct The_cow{
int s,e;
bool operator < (const The_cow &a) const {
if(a.s == s)
return a.e < e;
return a.s > s;
}
}cow[maxn];
int main() {
while(scanf("%d%d",&n,&t) != EOF) {
memset(cow,0,sizeof(cow));
for(int i=0;i<n;i++){
scanf("%d%d",&cow[i].s,&cow[i].e);
if(cow[i].s > cow[i].e)
swap(cow[i].s,cow[i].e);
}
sort(cow,cow+n);
if(cow[0].s != 1) {
printf("-1\n");
continue;
}
int ans = 1,pos_end = 0,temp_end = 0;//temp_end为当前记录的终点,pos_end是延伸出去的最长终点
temp_end = cow[0].e;
for(int i=1;i<n;i++){
if(cow[i].s > temp_end + 1){//为啥不加等号,因为在temp_end+1的位置的起点也可以用来更新pos_end
temp_end = pos_end;
ans++;
}
if(cow[i].s <= temp_end + 1) {
if(cow[i].e > pos_end)
pos_end = cow[i].e;
if(cow[i].e == t) {
ans ++;
temp_end = t;
break;
}
}
}
if(temp_end == t)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}
POJ:2367-Cleaning Shifts的更多相关文章
- 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 贪心
Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...
- 【原创】poj ----- 2376 Cleaning Shifts 解题报告
题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K ...
- poj 3171 Cleaning Shifts(区间的最小覆盖价值)
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2743 Accepted: 955 De ...
- 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 最小区间覆盖
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40751 Accepted: 9871 ...
- poj 2376 Cleaning Shifts(贪心)
Description Farmer John <= N <= ,) cows to <= T <= ,,), the first being shift and the la ...
- ACM学习历程——POJ 2376 Cleaning Shifts(贪心)
Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning ...
随机推荐
- vue 修饰符 整理
事件修饰符 .stop <!-- 阻止单击事件继续传播 --> <a v-on:click.stop="doThis"></a> .preven ...
- Android触摸事件MotionEvent详解
触摸事件MotionEvent在用户交互中,占着非常重要的地位.首先,来看看MotionEvent中封装的一些常用的事件常量,它定义了触摸事件的不同类型. 1.单点触摸按下动作 public stat ...
- [QualityCenter]QC是什么?发展历程是怎样?
QC,即Quality Center,是一个基于Web的测试管理工具.它可以组织和管理应用程序测试流程的所有阶段,包括制定测试需求.计划测试.执行测试和跟踪缺陷.此外,通过Quality Center ...
- GitHub教程(三) 本地仓库托管到GitHub
本文开头先特别声明一下:由于GitHub教程属于Git系列教程的GitHub子篇章,因此GitHub教程中将不再详细介绍Git操作命令及其用法,我会根据实际需要穿插着回顾Git操作命令.如果读者需要学 ...
- 【转载】每天一个Linux命令
目 录 每天一个linux命令(1) : ls 命令 每天一个linux命令(2) : cd 命令 每天一个linux命令(3) : pwd 命令 每天一个linux命令(4) : mkdi ...
- 计算后缀表达式的过程(C#)
计算后缀表达式的过程是一个很好玩的过程,而且很简单哦!这里呢,有个计算的技巧,就是:遇到数字直接入栈,遇到运算符就计算! 后缀表达式也叫逆波兰表达式,求值过程可以用到栈来辅助存储: 假定待求值的后缀表 ...
- 命令行输入Jmeter提示不是内部或外部命令,处理方式:添加环境变量
命令行输入Jmeter提示不是内部或外部命令,需要在环境变量path中添加jmeter的bin目录绝对路径 我的电脑 > 右击 >属性 > 高级系统变量 > 环境变量 > ...
- Jenkins添加项目说明,增加项目描述
背景:往往正常Jenkins上呈现的内容,太过简短,不易直观看了解项目是干嘛的,如下面的内容: 解决方案,使用插件,Extra Columns Plugin 安装成功后配置,需要结合自定义视图使用,新 ...
- ABI and compiler
http://stackoverflow.com/questions/2171177/what-is-application-binary-interface-abi ABIs cover detai ...
- 【洛谷4459】[BJOI2018] 双人猜数游戏(动态规划)
点此看题面 大致题意: 一直有两个数\(m,n\),已知\(s\le m\le n\),且\(Alice\)和\(Bob\)二个"最强大佬"各知道\(mn\)和\(m+n\).每轮 ...