poj 2376 Cleaning Shifts
http://poj.org/problem?id=2376
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12604 | Accepted: 3263 |
Description
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
* 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
Sample Input
3 10
1 7
3 6
6 10
Sample Output
2
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.
10 //3代表小区间个数,10代表大区间长度。
上面的数据我的程序输出的是:-1
因为有两个区间没有“完全覆盖”,[5 ,6]和[8 ,9]。
但是正确答案是 3 。
WA代码:
#include<cstdio>
#include<algorithm>
using namespace std;
struct P
{
int x,y;
bool operator < (const P & p) const
{
return x < p.x || (x == p.x && y > p.y);
}
}a[]; int main()
{
int n,t;
while(~scanf("%d %d",&n,&t))
{
for(int i = ;i < n;i++)
scanf("%d %d",&a[i].x,&a[i].y);
sort(a ,a + n);
int res = ,s;
if(a[].x > )
printf("-1\n");
else
{
s = a[].y;
for(int i = ;i < n && s < t;)
{
int tmp = ;
while(i < n && a[i].x <= s)
{
tmp = max(tmp , a[i].y);
i++;
}
if(tmp > s)
{
s = tmp;
res++;
}
else
break;
}
}
if(s >= t)
printf("%d\n",res);
else
printf("-1\n");
}
return ;
}
AC代码:
#include<cstdio>
#include<algorithm>
using namespace std; struct P{
int x,y;
bool operator < (const P & p) const {
return x < p.x || (x == p.x && y > p.y);
}
}a[]; int main() {
int n,t;
while(~scanf("%d %d",&n,&t)) {
for(int i = ;i < n;i++)
scanf("%d %d",&a[i].x,&a[i].y); sort(a ,a + n); int res = ,s;
if(a[].x > ) {
printf("-1\n");
continue;
} else {
s = a[].y;
for(int i = ;i < n && s < t;) {
int tmp = ;
while(i < n && a[i].x <= s + ) {
tmp = max(tmp , a[i].y);
i++;
}
if(tmp > s) {
s = tmp;
res++;
} else break;
}
} if(s >= t) {
printf("%d\n",res);
} else {
printf("-1\n");
}
}
return ;
}
poj 2376 Cleaning Shifts的更多相关文章
- POJ 2376 Cleaning Shifts(轮班打扫)
POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Farmer ...
- 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 2376 Cleaning Shifts【贪心】
POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...
- 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 ...
随机推荐
- 2016 Multi-University Training Contest 3
5/11 2016 Multi-University Training Contest 3官方题解 2016年多校训练第三场 老年选手历险记 暴力 A Sqrt Bo(CYD) 题意:问进行多少次开根 ...
- LightOJ1126 Building Twin Towers(DP)
题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1126 Description Professor Sofdor Al ...
- Struts2 整合Spring(Maven,注解版)
这两天正在试验Struts2与Spring框架的整合,和他们各自的“注解”.今天就总结一下这两个框架怎么用注解进行整合. 一,加入两者的依赖包,除了两者的必要依赖外,还需要导入struts2-spri ...
- react学习小结(生命周期- 实例化时期 - 存在期- 销毁时期)
react学习小结 本文是我学习react的阶段性小结,如果看官你是react资深玩家,那么还请就此打住移步他处,如果你想给一些建议和指导,那么还请轻拍~ 目前团队内对react的使用非常普遍,之 ...
- 利用轮播原理结合hammer.js实现简洁的滑屏功能
最近有个任务,做一个非常小的h5的应用,只有2屏,需要做横向的全屏滑动切换和一些简单的动画效果,之前做这种东西用的是fullpage.js和jquery,性能不是很好,于是就想自己动手弄一个简单的东西 ...
- ZeroMQ接口函数之 :zmq_z85_decode – 从一个用Z85算法生成的文本中解析出二进制密码
ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_z85_decode zmq_z85_decode(3) ØMQ Manual - ØMQ/4.1 ...
- linux shell trap的使用
原文地址:http://blog.sina.com.cn/s/blog_62eb16bb01014dbh.html 一. trap捕捉到信号之后,可以有三种反应方式: (1)执行一段程序来处理这一信号 ...
- Python In Action:三、稍稍扩展
#!/usr/bin/env python """Spare.py is a starting point for simple wxPython programs.&q ...
- (四)G1 garbage collector
g1专为大内存,多内核机型设计.可以兼顾高吞吐量和低暂停时间. g1将堆分为多个相同大小内存块,并发的标记线程,使得g1掌握了各个内存块的活对象数量, 内存回收阶段,g1根据用户指定的暂停时间,选择部 ...
- springboot+solr
整合完DB层,cache层,开始整合solr. 注入SolrClient, package hello.configuration; import java.net.MalformedURLExcep ...