POJ3190:Stall Reservations(贪心)

Time Limit: 1000MS
Memory Limit: 65536K
Special Judge


Description

Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows.

Help FJ by determining:

  • The minimum number of stalls required in the barn so that each cow can have her private milking period
  • An assignment of cows to these stalls over time
    Many answers are correct for each test dataset; a program will grade your answer.

Input

Line 1: A single integer, N

Lines 2..N+1: Line i+1 describes cow i’s milking interval with two space-separated integers.

Output

Line 1: The minimum number of stalls the barn must have.

Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.

Sample Input

5
1 10
2 4
3 6
5 8
4 7

Sample Output

4
1
2
3
2
4

Hint

Explanation of the sample:

Here’s a graphical schedule for this output:

这里写图片描述

Other outputs using the same number of stalls are possible.

Source

USACO 2006 February Silver


解题分析

所有奶牛都必须挤奶。到了一个奶牛的挤奶开始时间,就必须为这个奶牛找畜栏。因此按照奶牛的开始时间逐个处理它们,是必然的。

S(x) 表示奶牛 x 的开始时间, E(x) 表示 x 的结束时间。对 E(x) x 可以是奶牛,也可以是畜栏。畜栏的结束时间,就是正在其里面挤奶的奶牛的结束时间。同一个畜栏的结束时间是不断在变的。

1)把所有奶牛按开始时间从小到大排序。
2)为第一头奶牛分配一个畜栏。
3)依次处理后面每头奶牛 i 。处理i时,考虑已分配畜栏中,结束时间最早的畜栏 x

  • E(x)<S(i) ,则不用分配新畜栏, i 可进入 x ,并修改 E(x) E(i)
  • E(x)>=S(i) ,则分配新畜栏 y ,记 E(y)=E(i)

直到所有奶牛处理结束。

需要用优先队列存放已经分配的畜栏,并使得结束时间最早的畜栏始终位于队列头部。

证明:
由于按开始时间的顺序处理奶牛是必然,且按该算法,为奶牛 i 分配新畜栏时,确实是不得不分配的,所以算法正确。

复杂度:
O(nlogn)


AC代码

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

struct Cow{
    int a,b;
    int No;
    bool operator < (const Cow &c)
    const{return a < c.a;}
}cows[50100];
int pos[50100];

struct Stall{
    int et;
    int No;
    bool operator < (const Stall &s)
    const{return et > s.et;}
    Stall(int e,int n):et(e),No(n){}
};
priority_queue<Stall>pq;

int main()
{
    int N;
    scanf("%d",&N);
    for(int i=0;i<N;i++){
        scanf("%d%d",&cows[i].a,&cows[i].b);
        cows[i].No=i;
    }
    sort(cows,cows+N);
    int total=0;
    for(int i=0;i<N;i++){
        if(pq.empty()){
            total++;
            pq.push(Stall(cows[i].b,total));
            pos[cows[i].No]=total;
        }
        else{
            Stall st=pq.top();
            if(st.et<cows[i].a){
                pq.pop();
                pq.push(Stall(cows[i].b,st.No));
                pos[cows[i].No]=st.No;
            }
            else{
                total++;
                pq.push(Stall(cows[i].b,total));
                pos[cows[i].No]=total;
            }
        }
    }
    printf("%d\n",total);
    for(int i=0;i<N;i++)
        printf("%d\n",pos[i]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Hrbust_cx/article/details/78175117