Stall Reservations - 贪心 - 分配时间问题

题目

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.
噢,那些挑剔的牛!它们是如此挑剔,以至于每头奶牛只能在某个精确的时间间隔A…B(1<=A<=B<=1000000)内挤奶,这包括A和B两个时间间隔。显然,FJ必须创建一个预约系统来确定每头奶牛的挤奶时间可以分配到哪个摊位。当然,没有一头母牛会和其他母牛分享这样的私人时刻。

通过确定以下内容帮助FJ:
为了让每头母牛都能有自己的挤奶期,谷仓所需的最小摊位数
随着时间的推移牛被分配到这些摊位
对于每个测试数据集,许多答案都是正确的;程序将对您的答案进行评分。

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.
第1行:单个整数,N

第2…N+1行:第i+1行用两个空格分隔的整数描述cow i的挤奶间隔。

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.
第1行:谷仓必须拥有的最小摊位数。

第2…N+1行:第i+1行描述了奶牛挤奶期间我将被分配到的摊位。

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.
使用相同数量的暂停的其他输出是可能的。

#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int MAX = 50010;
struct qu
{
    int end;
    int id;
    bool operator<(const qu e) const
    {
        // 从小到大
        return end > e.end;
    }
};
struct cow
{
    int begin, end;
    int id;
    bool operator<(const cow &e) const
    {
        // 由小到大
        return begin < e.begin;
    }
} cows[MAX];
int main()
{
    int N;
    scanf("%d", &N);
    for (int i = 1; i <= N; i++)
    {
        scanf("%d%d", &cows[i].begin, &cows[i].end);
        cows[i].id = i;
    }
    sort(cows + 1, cows + N + 1);
    priority_queue<qu> q;
    int cnt = 1;
    // 记录第i个牛的畜栏编号
    int pos[MAX] = {0};

    // 注 因为这个题编译器的版本不支持
    // 所以就先存到一个变量里,再进行push了
    qu t;
    t.end = cows[1].end;
    // id 是畜栏的编号
    t.id = 1;
    q.push(t);
    pos[cows[1].id] = 1;

    for (int i = 2; i <= N; i++)
    {
        qu t = q.top();
        // 现在畜栏中结束时间最早的 比 现在的牛的开始时间要早
        // 所以这个牛可以去这个畜栏中
        if (t.end < cows[i].begin)
        {
            // 修改结束时间
            q.pop();
            // 第i头牛在t.id号畜栏
            pos[cows[i].id] = t.id;

            t.end = cows[i].end;
            q.push(t);
        }
        // 结束时间最早的都比i头牛的开始时间晚
        // 所以只能另开一个了
        else if (t.end >= cows[i].begin)
        {
            cnt++;

            t.end = cows[i].end;
            t.id = cnt;
            q.push(t);
            pos[cows[i].id] = cnt;
        }
    }
    cout << cnt << endl;
    for (int i = 1; i <= N; i++)
    {
        cout << pos[i] << endl;
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章