POJ 2376 Cleaning Shifts 区间覆盖

Cleaning Shifts
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16264   Accepted: 4148

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.

Source

USACO 2004 December Silver

点击打开题目链接

POJ 2376 Cleaning Shifts 区间覆盖_第1张图片


一切尽在代码中。。。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;

const int MAXN = 25000 + 10;

struct Cow
{
    int Start, End; //重载运算符,使之先按开始时间从小到大排序
    bool operator < (const Cow& t) const    //开始时间相同时再按结束时间从小到大排序
    {
        if (Start != t.Start)
            return Start < t.Start;
        return End < t.End;
    }
} cow[MAXN];

int main()
{
    int N, T;
    while (~scanf("%d%d", &N, &T))
    {
        for (int i = 0; i < N; i++)
            scanf("%d%d", &cow[i].Start, &cow[i].End);
        sort(cow, cow + N);             //排序
        int time, id, tid, tend;
        if (cow[0].Start != 1)          //第一个区间的起点不为1,无解
        {
            printf("-1\n");
            continue;
        }
        else
        {
            time = cow[0].End;      //当前时间
            id = 1;                 //记录起点为1,结束时间最大的区间
            while (cow[id].Start == 1)
            {
                time = cow[id].End; //更新当前时间
                id++;
            }
        }
        int ans = 1;
        while (time < T)
        {
            if (id >= N) break; //所有区间都判断完毕
            tid = id;           //贪心法查找下一个区间,取合法区间中结束时间最大的那个
            tend = cow[id].End;
            id++;
            while (id < N && cow[id].Start <= time + 1) //不断查找下一个区间
            {
                if (cow[id].End > tend)     //结束时间还可以变大
                {
                    tid = id;
                    tend = cow[id].End;
                }
                id++;
            }
            if (tend <= time || cow[tid].Start > time + 1) break;   //结束时间没有增长或区间非法
            else
            {
                ans++;          //增加一个区间,当前时间变为选择区间的结束时间
                time = cow[tid].End;
            }
        }
        if (time < T)
            printf("-1\n");
        else
            printf("%d\n", ans);
    }
    return 0;
}


你可能感兴趣的:(ACM,贪心)