POJ Stall Reservations

Stall Reservations

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

Time 1 2 3 4 5 6 7 8 9 10

Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>

Stall 2 … c2>>>>>> c4>>>>>>>>> … …

Stall 3 … … c3>>>>>>>>> … … … …

Stall 4 … … … c5>>>>>>>>> … … …
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)直到所有奶牛处理结束需要用优先队列存放已经分配的畜栏,并使得结束时间最早的畜栏始终位于队列头部。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define ll long long
#define mes(x,y) memset(x,y,sizeof(x))
using namespace std;
struct node{
    ll star,end,number;
    bool operator<(const node & c) const {
        return star < c.star;
    }
}a[50100];
struct nove{
    ll end,number;
    bool operator<(const nove & s) const {
        return end > s.end;
    }
}nv;
int main(){
    ll n,i,j,k,sum=1;
    while(scanf("%lld",&n)!=EOF) {
        sum=1;
        priority_queue<nove> que;
        map<ll, ll> mm;mm.clear();
        for (i = 0; i < n; i++) {
            scanf("%lld %lld", &a[i].star, &a[i].end);
            a[i].number = i + 1;
        }
        sort(a, a + n);
        mm[a[0].number] = 1;
        nv.end = a[0].end;
        nv.number = 1;
        que.push(nv);
        for (j = 1; j < n; ++j) {
            nv = que.top();
            if (a[j].star <= nv.end) {
                sum++;
                nv.end = a[j].end;
                nv.number = sum;
                que.push(nv);
                mm[a[j].number] = sum;
            } else {
                nv.end = a[j].end;
                que.push(nv);
                que.pop();
                mm[a[j].number] = nv.number;
            }
        }
        cout << sum << endl;
        for (k = 1; k <= n; ++k) {
            cout << mm[k] << endl;
        }
    }
}

发布了148 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44417851/article/details/91462241