Cleaning Shifts

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 40751   Accepted: 9871

Description

Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T.

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

* Line 1: Two space-separated integers: N and T

* 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

* Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.

Sample Input

3 10
1 7
3 6
6 10

Sample Output

2

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

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.

 
题意:有n头牛要值班,每头牛有一个值班区间,要安排牛在[1 , k]区间值班,最少需要几头牛
 
题解:左端点不相等,按从小到大排序,相等按右端点从大到小排序。for循环遍历,在左端点>=now+1的区间中选右端点最大的区间即可
 
#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 最小区间覆盖的更多相关文章

  1. poj 2376 Cleaning Shifts 贪心 区间问题

    <pre name="code" class="html"> Cleaning Shifts Time Limit: 1000MS   Memory ...

  2. POJ 2376 Cleaning Shifts(轮班打扫)

    POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Farmer ...

  3. poj 2376 Cleaning Shifts

    http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  4. POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)

    Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...

  5. POJ 2376 Cleaning Shifts 区间覆盖问题

    http://poj.org/problem?id=2376 题目大意: 给你一些区间的起点和终点,让你用最小的区间覆盖一个大的区间. 思路: 贪心,按区间的起点找满足条件的并且终点尽量大的. 一开始 ...

  6. POJ 2376 Cleaning Shifts (贪心,区间覆盖)

    题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小 ...

  7. POJ 2376 Cleaning Shifts 贪心

    Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...

  8. 【原创】poj ----- 2376 Cleaning Shifts 解题报告

    题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K ...

  9. poj 2376 Cleaning Shifts(贪心)

    Description Farmer John <= N <= ,) cows to <= T <= ,,), the first being shift and the la ...

随机推荐

  1. 132、Java面向对象之static关键字四(定义一个数学的加法操作)

    01.代码如下: package TIANPAN; class MyMath { // 数学操作类,类中没有属性 public static int add(int x, int y) { // 只是 ...

  2. Solr与tomcat搭建(搭建好)

    https://pan.baidu.com/s/1kXagNYJ  密码:hgxd

  3. autoit 《FAQ 大全》

    常见问题:  Q1 如何调试脚本? MsgBox(0,"测试",$var) ConsoleWrite("var=" & $var & @CRLF ...

  4. python的沙盒环境--virtualenv

      VirtualEnv用于在一台机器上创建多个独立的python运行环境,VirtualEnvWrapper为前者提供了一些便利的命令行上的封装. 使用 VirtualEnv 的理由: 隔离项目之间 ...

  5. unity渲染优化

    https://blog.csdn.net/yudianxia/article/details/79339103 https://blog.csdn.net/e295166319/article/de ...

  6. Vuex踩坑--数据刷新时丢失

    近期做项目的过程中,使用vuex保存页面公共数据,测试无网情况后又接通网络的情况下,页面进行重新加载.遇到一个小bug——发现在苹果手机IOS系统下,页面刷新重新加载后页面中通过vuex存储并显示的数 ...

  7. 【剑指Offer面试编程题】题目1283:第一个只出现一次的字符--九度OJ

    题目描述: 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符. 输入: 输入有多组数据 每一组输入一个字符串. 输出: 输出第一个只出现一次的 ...

  8. 记录一次Git远程仓库版本回退

    操作过程: 首先查看远程仓库版本,如下图所见,最近一次提交为2018-03-19 22:16:25 第一步:使用git log命令查看历史提交记录,选择要回退的版本号,commit后面一串字符,这里我 ...

  9. Linux进程通信方式

    参考:https://www.cnblogs.com/yangykaifa/p/7295863.html

  10. Windows驱动开发-派遣函数

    一个简单的派遣函数格式 NTSTATUS DispatchFunction(PDEVICE_OBJECT pDeviceObject, PIRP pIrp) { //业务代码区 //设置返回状态 pI ...