poj 3171 Cleaning Shifts

Cleaning Shifts

                                             Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u


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.


思路:既然要求覆盖 1 到 m 至少需要多少小区间,因此需要对每个区间起点按从小到大开始排序
如果起点区间相同,就让结束点按从大到小排序,排好序之后,判断如果第一个区间起点不是 1,
或者,结束点包不包含所要覆盖的长度,如果不包含 就输出-1;如果满足所给的条件就使第一个区间

的结束点依此与下一个区间的开始点作比较,如果满足下一个开始点小于第一个区间的结束点+1,

就找到最大的结束点,把这个结束点当作第二次区间的结束点,依次类推

#include<stdio.h>
#include<algorithm>
using namespace std;
struct v
{
    int begin;
    int end;
} a[25100];
bool cmp(const v x,const v y)
{
    if(x.begin==y.begin)          //如果开始点相同,结束点从大到小排
        return x.end>y.end;
    return x.begin<y.begin;         //开始点从小到大排
}
int main()
{
    int m,n;
    while(~scanf("%d%d",&n,&m))
    {
        int i,count=1,flag=1,j=0,tx,ty,k=0,f=0;
        for(i=0; i<n; i++)
            scanf("%d%d",&a[i].begin,&a[i].end);
        sort(a,a+n,cmp);
        for(i=0; i<n; i++)        //找有没有符合提议的点
            if(a[i].end>=m)
                f=1;
        if(a[0].begin!=1||!f)//如果没有,输出-1
        {
            printf("-1\n");
            continue;
        }
        if(a[0].begin==1&&a[0].end>=m)  //如果第一组数就符合题意,就结束
        {
            printf("%d\n",count);
            continue;
        }
        tx=a[0].end;
        while(tx<m&&k<n)      //结束条件
        {
            ty=0;
            for(j=k+1; a[j].begin<=tx+1&&j<n; j++ )//如果开始的值小于上一个区间的结束点的值
            {
                if(a[j].end>ty)
                {
                    ty=a[j].end;  //把结束点比较大的值赋给ty
                    k=j;
                }
            }
            tx=ty;
            if(1+tx<a[k+1].begin)  //如果中间有间断点,说明不符合题意
            {
                flag=0;
                break;
            }

            count++;//符合题意  计数
        }
        if(flag==0)  //如果flag为0说明没有符合题意的
            printf("-1\n");
        else
            printf("%d\n",count);  //如果有 输出统计数
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值