poj 2376 Cleaning Shifts 最小区间覆盖
Cleaning Shifts
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 40751 | Accepted: 9871 |
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.
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<stack>
#include<math.h>
#define mod 998244353
#define ll long long
#define MAX 0x3f3f3f3f
using namespace std;
int vis[];
struct node
{
int l;
int r;
}p[]; bool cmp(node a,node b)
{
if(a.l!=b.l)
return a.l<b.l;
else
return a.r>b.r;
}
int main()
{
int n,t;
while(~scanf("%d%d",&n,&t))
{
int flag=,cnt=,pos=;
for(int i=;i<n;i++)
scanf("%d%d",&p[i].l,&p[i].r);
sort(p,p+n,cmp);
if(p[].l!=)
flag=;
int now=p[].r;
for(int i=;i<n&&flag==;)
{
if(p[i].l>now+)
{
flag=;
break;
}
else
{
int temp=now;
while(i<n&&p[i].l<=now+)//在左端点<=now+1的区间中选右端点最大的区间
{
if(p[i].r>temp)//更新最大右端点
{
temp=p[i].r;
pos=i;
}
i++;
}
if(temp==now)//更新之前和更新之后一样,那还不如不更新
continue;
now=temp;//更新
cnt++;
}
}
if(p[pos].r!=t)
flag=;
if(flag==)
printf("-1\n");
else
printf("%d\n",cnt );
}
return ;
}
poj 2376 Cleaning Shifts 最小区间覆盖的更多相关文章
- poj 2376 Cleaning Shifts 贪心 区间问题
<pre name="code" class="html"> Cleaning Shifts Time Limit: 1000MS Memory ...
- 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 Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...
- POJ 2376 Cleaning Shifts 区间覆盖问题
http://poj.org/problem?id=2376 题目大意: 给你一些区间的起点和终点,让你用最小的区间覆盖一个大的区间. 思路: 贪心,按区间的起点找满足条件的并且终点尽量大的. 一开始 ...
- POJ 2376 Cleaning Shifts (贪心,区间覆盖)
题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小 ...
- 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(贪心)
Description Farmer John <= N <= ,) cows to <= T <= ,,), the first being shift and the la ...
随机推荐
- cin和cout输⼊输出
写再最前面:摘录于柳神的笔记: 就如同 scanf 和 printf 在 stdio.h 头⽂件中⼀样, cin 和 cout 在头⽂件 iostream ⾥⾯,看名字就知 道, io 是输⼊输出 ...
- Python数据类型-3 布尔类型
布尔类型 对于错.0和1.正与反,都是传统意义上的布尔类型. 但在Python语言中,布尔类型只有两个值,True与False.请注意,是英文单词的对与错,并且首字母要大写,不能其它花式变型. 布尔值 ...
- tomcat启动报错failed to start component
严重: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catali ...
- Controller 层类
package com.thinkgem.jeesite.modules.yudengji.web; import java.util.Date; import javax.servlet.http. ...
- Windows服务器权限分析
一.Windows常见用户 二.Windows常见用组 2.1Windows常见组 2.2Windows2003常见组 三.Windows目录权限 四.Windows2003默认权限 五.不同环境下的 ...
- 使用oracle 的 PL/Sql 定时执行一个存储过程
CSDN日报20170322--<关于软件研发的一些体会总结> 同步博客至 CSDN ,让更多开发者看到你的文章 看微博技术大咖解析互联网应用架构实战 使用oracle 的 PL/Sql ...
- UI的考核方式之【主题颜色 + 主题字体 + 主题样式】
首先,项目中的颜色需要怎么管理呢? 宏?常量? 宏的值可以被改变,不用宏.那就用常量.那常量的名字怎么去起名呢? WSFConstants_Color_0xCCCCCC? WSFConstants_C ...
- 【快学springboot】在springboot中写单元测试
前言 很多公司都有写单元测试的硬性要求,在提交代码的时候,如果单测通不过或者说单元测试各种覆盖率不达标,会被拒绝合并代码.写单元测试,也是保证代码质量的一种方式. junit单元测试 相信绝大多数的J ...
- A. You Are Given Two Binary Strings…
A. You Are Given Two Binary Strings… You are given two binary strings x and y, which are binary repr ...
- 夯实Java基础(十八)——泛型
1.什么是泛型 泛型是Java1.5中出现的新特性,也是最重要的一个特性.泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以用在类.接口和方法的创建中,分别称为泛型类. ...