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;
}

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