Cleaning Shifts(POJ_2376) 【贪心、区间】

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.
    Sampe 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.

题意描述:
FJ分配 N (1 <= N <= 25,000) 只中的一些奶牛在牛棚附近做些清洁。 他总是要让至少一只牛做清洁。他把一天分成T段(1 <= T <= 1,000,000), 第一段是1,最后一段是T
每只奶牛只在一些时间段有空。奶牛如果选择某一段时间,则必须完成整段时间的工作
你的任务是帮助FJ安排一些奶牛,使每段时间至少有一只奶牛被安排来做这件事。并且奶牛数应尽可能小。如果不可能办到,输出-1

分析:

有N头牛,其土地的耕作时间是1-T,给出每头牛的工作区间,问最少需要几头牛可以满足全部耕作时间都至少有一头牛在工作。

标准区间贪心,先按照每头牛的开始时间排序,每次选开始时间小于未覆盖区间的开始时间的牛中,结束时间最大的那个。

用小区间去覆盖大区间!

代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node{
    int start,end;
} str[25100];
int cmp(node A,node B){
    if(A.start!=B.start) //左端点从小到大
        return A.start<B.start;
    else    							//右端点从大到小,便于选取尽量少的区间
        return A.end>B.end;
}
int main(){
    int m,n,i,j,k,t;
    while(~scanf("%d%d",&m,&n)){
        for(i=0; i<m; i++)
            scanf("%d%d",&str[i].start,&str[i].end);//输入区间的左右端点
        sort(str,str+m,cmp);
        if(str[0].start!=1)  //如果第一个区间的左端点没有覆盖到边界,说明无法完全覆盖
        {
            printf("-1\n");
            continue;
            
        }
        j=0;//更新右端点值
        k=1;
        t=str[0].end;
        for(i=1; i<m; i++){
            if(str[i].start>t+1) //一定要是t+1
            {
                t=j;
                k++;
            }
            if(str[i].start<=t+1){
                if(str[i].end>j) //如果右端点大于现在的右最大值,最大值为它
                    j=str[i].end;
                if(str[i].end==n)  //如果右端点等于边界,说明完全覆盖
                {
                    k++;       //区间加1,说明是最后一个区间
                    t=n;     //把边界赋给t,便于跳出循环
                    break;
                }
            }
        }
        if(t==n)   printf("%d\n",k);
        else       printf("-1\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一倾而尽

你的鼓励将是我最大的动力,谢谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值