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 ...
随机推荐
- Go的WaitGroup
goroutine使用方便,但是如果不加以处理一般会deadlock,因为goroutine配合Chanel的话只能是一进一出,否则就会卡在那里.下面一个示例就是利用这个WaitGroup处理这种死锁 ...
- linux面试经验
互联网面试想必是每个学计算机的学生必不可少的环节,无论你的项目经验再多,你不准备基础知识,也还是无济于事.首先来说说关于工作的事情. 三年前,那时候我还是刚刚快要大四毕业的小鲜肉,那时候有个超大的招聘 ...
- Intellij Idea 下包建包,无论怎么建都在同一级,已解决(附图)
1.很多新手,刚开始使用Intellij Idea的时候,项目建包都出现所建的包都在用一级. 2.这是因为,刚开始建项目的时候,Hide Empty Middle Packages是默认勾选的,只要去 ...
- error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 解决方法
在VS2012中生成时出错:error C4430: missing type specifier - int assumed. Note: C++ does not support default- ...
- mac下安装并启动RabbitMQ
前言 RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件).RabbitMQ服务器是用Erlang语言编写的,而群集和故障转移是构建在开放电信平台框架上的 ...
- 2020-2-18 restful的学习
1-1 restful简介及资源的介绍 restful 是什么? 本质:一种软件架构风格 核心:面向资源 解决的问题: 1. 降低开发的复杂性 2. 提高系统的可伸缩性 设计概念和 ...
- pytorch深度学习神经网络实现手写字体识别
利用平pytorch搭建简单的神经网络实现minist手写字体的识别,采用三层线性函数迭代运算,使得其具备一定的非线性转化与运算能力,其数学原理如下: 其具体实现代码如下所示:import torch ...
- Linux CentOS7 VMware克隆、虚拟机之间互连——初学笔记
一.VMware克隆:选中我的第一个虚拟机,右键下拉菜单—管理—克隆. 弹出对话框,下一步根据提示: 完成克隆: 二.虚拟机互连: 配置第二虚拟机IP,使用:vi /etc/s ...
- 原生JS获取所有标签的数量并统计每个标签的数量
<script type="text/javascript"> var tags = document.getElementsByTagName('*'); var t ...
- Codeforces Round #199 (Div. 2) D. Xenia and Dominoes
把 'O' 看成 'X',然后枚举它的四个方向看看是否能放,然后枚举 $2^4$ 种可能表示每种方向是否放了,放了的话就标成 'X',就相当于容斥,对于新的图去dp. dp就是铺地砖,行用二进制来表示 ...