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 ...
随机推荐
- Linux UBI子系统设计初探
问题领域 flash存储设备存在如下特点: 存在坏块 使用寿命较短 存储介质不稳定 读写速度慢 不支持随机访问(nand) 只能通过擦除将0改成1 最小读写单位为page or sub-page 便宜 ...
- YUM源设置
1挂载光盘 先创建一个文件 /aaa 然后挂载mount /dev/cdrom /aaa 进入 /aaa ls 查看是否挂载OK 2进入yum文件夹.将除Media以外的所有文件名改为XXXXXX ...
- 关于 error: Operation is not valid due to the current state of the object。
今天碰到一个特别的异常. Operation is not valid due to the current state of the object. at System.Web.HttpValueC ...
- MongoDB基本使用
成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作. 输入help可以看到基本操作命令: show dbs:显示数据库列表 show collections:显示 ...
- PHP连接数据库的方法
mysql可通过两种方式通过PHP和web相连,一种通过php的mysql相关函数,另一种通过php的ODBC相关函数. 相关函数如下: MYSQL函数 mysql_affected_rows: 得到 ...
- quick-3.5 eclipse android
quick-3.5 eclipse android 上遇到的 问题 首先是已经安装了SDK NDK ADT 环境变量都已经配置好了 直接打开项目然后运行 却出现这个鸟问题如图: NDK_ROOT = ...
- Jvascript简介
一.Javascript就是我们所说的脚本语言.它不同于C++/java等语言,它更加灵活! 正因为其灵活性,没有那么多的规章制度,也是我们容易学的地方,但很多时候也是 令人无奈的地方! 二.我们要知 ...
- PHPUnit入门
PHPUnit是PHP语言的单元测试框架.工具,xunit单元测试工具系列成员之一,可以单独运行在Linux或windows系统下面,也可以集成到zend studio等IDE工具中. 工具下载:ht ...
- poj3740
#include<stdio.h>#include<iostream>int map[16][300];int vistx[16];int visty[300];int zhe ...
- java 特性
1.不支持默认参数 2. 没有指针.malloc.free .内存会自动回收 3. 解释性语言,必须跑在虚拟机上,可以实现跨平台