AYITACM2016省赛第二周E - Cleaning Shifts(区间覆盖)

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.


分析:

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

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

用小区间去覆盖大区间!

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



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值