POJ - 2376 Cleaning Shifts【哈希一下】

时间:2022-10-15 04:22:55

Cleaning Shifts

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 25403 Accepted: 6299
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

题意: 给你n个地点和终点,和范围[1.T],问你最少取组起点终点,使得[1,T]都能被覆盖掉。

分析: 直接将起点终点hash下,其实也可以排下序,看了下范围1e6,hash没问题,预处理下即可,先特判下是否覆盖了1和T这两个点,然后再看中间是否有没有被覆盖的,count下即可

参考代码

#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1e5 + 10, INF = 0x3f3f3f3f;

int h[1000010];

struct f{
    int l,r;
}a[30000];

int main(){
    ios_base::sync_with_stdio(0);
    int n,t;
    while(cin>>n>>t) {
        for(int i = 0;i <= 1000000;i++) h[i] = 0;
        int mi = INF,ma = -INF;
        for(int i = 0;i < n;i++) {
            cin>>a[i].l>>a[i].r;
            h[a[i].l] = max(h[a[i].l],a[i].r);
            mi = min(mi,a[i].l);
            ma = max(ma,a[i].r);
        }
        bool flg = false;
        int res = 1;
        if(mi > 1 || ma < t) {
            flg = true;
        } else {
            int L = 1,R = h[1];
            while(true) {
                if(R >= t) {
                    break;
                }
                int RR = -1;
                for(int i = L;i <= R+1;i++) {
                    if(h[i] > R) {
                        RR = max(RR,h[i]);
                    }
                }
                if(RR == -1) {
                    flg = true;
                    break;
                }
                res++;
                if(RR < t) {
                    L = R;
                    R = RR;
                } else {
                    break;
                }
            }
        }
        if(flg) cout<<-1<<endl;
        else cout<<res<<endl;
    }
    return 0;
}
  • 如有错误或遗漏,请私聊下UP,thx