天天看点

POJ_2376_Cleaning Shifts(贪心)

Cleaning Shifts

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12788 Accepted: 3312

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头牛。现在他想让一些牛去做家务。然后他把一天分成T个时间点,也就是一天的时间点是区间[1,T]。他想要任何一个时间点都有牛在做家务。现在给出每头牛的工作时间,问你能否用最少的牛满足他的要求,即用最少的时间段覆盖掉这一天([1,T])。如果不能满足则输出-1,否则输出最少的牛数量。

分析:贪心题。题目意思很明显是求最少的区间覆盖掉大区间。先对这些时间段排好序(见代码),这个排序应该是没什么问题的。然后呢,第一头牛肯定要选,就从这头牛开始,选取下一头牛。下一头牛怎么选取呢?即在满足条件的牛里面(注意:满足条件的牛是只要开始工作时间 start>=cow[0].y+1 即可),选取右边界值最大的那个,因为这样子能够覆盖掉最多的时间段。以此类推,故贪心法求之。

题目链接: http://poj.org/problem?id=2376

代码清单:

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<cctype>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;

const int maxn = 25000 + 5;
const int maxd = 1000000 + 5;

struct Edge{
    int x;
    int y;
}cow[maxn];

int N,T;
int X,Y;

bool cmp(Edge a,Edge b){
    if(a.x==b.x) return a.y>b.y;
    return a.x<b.x;
}

void input(){
    X=0; Y=0;
    scanf("%d%d",&N,&T);
    for(int i=0;i<N;i++){
        scanf("%d%d",&cow[i].x,&cow[i].y);
        if(cow[i].x==1) X=1;
        if(cow[i].y==T) Y=1;
    }
}

void solve(){
    sort(cow,cow+N,cmp);
    //若所有的牛的工作时间的起始时间和结束时间都没有覆盖掉区间的起始时间和结束时间
    if(X==0 || Y==0){
        printf("-1\n");
        return ;
    }
    int ans=1,End=0,sum=0;
    int Start=cow[0].y;
    int maxy=cow[0].y;
    while(true){
        while(End+1<N&&cow[End+1].x<=Start+1){
            End++;
            if(cow[End].y>maxy){
                maxy=cow[End].y;
            }
        }
        if(maxy!=Start){
            ans++;
            Start=maxy;
        }
        else{
            if(End==N-1){ //已覆盖掉区间
                break;
            }
            else{  //说明中间有的时间点覆盖不到
                printf("-1\n");
                return ;
            }
        }
    }
    printf("%d\n",ans);
    return ;
}

int main(){
    input();
    solve();
    return 0;
}