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 ...
随机推荐
- MNIST手写数字分类simple版(03-2)
simple版本nn模型 训练手写数字处理 MNIST_data数据 百度网盘链接:https://pan.baidu.com/s/19lhmrts-vz0-w5wv2A97gg 提取码:cgnx ...
- Linux 修改/etc/profile 出错 导致所有命令都 command not found 的解决办法
执行命令 export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin 然后再修改/etc/profile 再执行文件: source /etc/p ...
- VS Code 入门
将VSCode设置成中文语言环境 快捷键[Ctrl+Shift+P]—输入[Configure Display Language]—将“en”改为“zh-CN”—打开extention输入[Chine ...
- 【PAT甲级】1016 Phone Bills (25 分)(结构体排序)
题意: 输入24个正整数代表从0到23每个小时通话一分钟花费的美分.输入一个正整数N(<=1000),然后输入N组字符串,每个字符串包含客户的名字和通话的时刻以及打出或者挂断的状态. 按照字典序 ...
- JS - 处理浏览器兼容之 event
function test(e){ var event = e || windows.event // IE : windows.event ,非IE : e }
- 2.分析Ajax请求并抓取今日头条街拍美图
import requests from urllib.parse import urlencode # 引入异常类 from requests.exceptions import RequestEx ...
- PLSQL Developer常用设置及快捷键
CSDN日报20170314--<40岁程序员真的要被淘汰了么?> 程序员2月书讯 [直播]用面向协议的思想简化网络请求 博客一键搬家活动开始啦 PLSQL Developer常用设置及快 ...
- PyCharm底部控制台console界面开启/取消自动换行
File --> Settings --> Editor --> General --> Console中 勾选右侧第一项Use soft wraps in console(选 ...
- PHP pclzip.php 解压中文乱码
修改 pclzip中方法privExtractFile 代码 if ($p_path != '') { $p_entry['filename'] = $p_path."/".$p_ ...
- Screen命令让Linux shell在后台运行
#screen ping ychack.com //挂置后台ping本站 #screen ping baidu.com //挂置后台ping百度 #screen -ls //列出进程 #screen ...