poj 2376 Cleaning Shifts 区间覆盖

来源:互联网 发布:网络产品线是干什么的 编辑:程序博客网 时间:2024/06/05 03:22

Cleaning Shifts
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17106 Accepted: 4366
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

#include <iostream>#include <cstdio>#include <string.h>#include <climits>#include <algorithm>using namespace std;const int N = 25005;struct intversal{    int begin,end;} aa[N];bool cmp(intversal a,intversal b){    if(a.begin == b.begin)        return a.end < b.end;    return a.begin < b.begin;}int main(){    int n,T;    while(scanf("%d%d",&n,&T) != EOF)    {        for(int i = 0; i < n; ++i)            scanf("%d%d",&aa[i].begin,&aa[i].end);        sort(aa,aa+n,cmp);        if(aa[0].begin > 1)        {            printf("-1\n");            continue;        }        int ans = 1,id = 0;        for(int i = 0; i < n; )        {            int cnt = 0;            for(int j = i + 1; j < n; ++j)            {                if(aa[j].begin > aa[id].end + 1)                    break;                if(aa[j].begin >= aa[id].begin && aa[j].end >= aa[id].end + 1)                {                    if(aa[j].end > aa[cnt].end)                        cnt = j;                }            }            if(cnt == 0)            {                i++;            }            else            {                id = cnt;                ans++;                i = id;            }        }        if(aa[id].end == T)        {            printf("%d\n",ans);        }        else            printf("-1\n");    }    return 0;}
0 0