poj-3190 Stall Reservations 贪心+优先队列

题目链接

poj-3190 Stall Reservations

题目

Stall Reservations
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8938 Accepted: 3131 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

题意

有N头奶牛,每头奶牛都会在[1,1000000]的时间区间内的子区间进行挤奶。挤奶的时候奶牛一定要单独放在一个牛棚中。一头奶牛的结束时间与另一头奶牛的开始时间重合的时候2头奶牛不能放在同一个牛棚中,例A牛时间[5,10],B牛时间[10,12],那么需要用2个牛棚放这2头牛。输入N头牛的所有的挤奶时间区间,要求输出最少要建几个牛棚,以及每头牛挤奶的时候在第几号牛棚。

题解

其实这个题考虑贪心的话还是比较简单的。考虑实际上我们应该如何规划这个问题:将所有奶牛根据挤奶开始时间排序,从最早开始挤奶的奶牛开始放牛棚。然后一个一个拉出新的奶牛,如果恰好由牛棚是空的,就放新的奶牛,否则就建新的牛棚。

这个题的思想还是相对简单的贪心,就是按照常规的思路看最少能建几个牛棚,本质也是最大化利用空的牛棚。这个题比较难的点在于编码上。刚开始博主确实感觉有思路但是不知道从哪里开始下手写代码(C++太水)。这里分析一下:

1.奶牛的数据结构是一定要有的,放开始时间、结束时间、编号。

2.奶牛是要根据挤奶开始时间排序的,因为我们要用贪心,就从最早开始挤奶的奶牛一个一个往下遍历。考虑如何分配牛棚。

3.考虑牛棚的数据结构,牛棚在使用的时候只用考虑当前牛棚的结束使用时间——即用这个牛棚的奶牛的挤奶结束时间。那么我们在判定是否有空牛棚的时候,可以将新奶牛的挤奶开始时间与所有牛棚中结束时间最早的进行比较。如果牛棚结束时间小于(注意不能等于)奶牛的开始时间,那么该牛棚就是空着的,可以被新奶牛使用。

4.考虑3.的问题,我们可以用优先队列来储存所有牛棚,以牛棚的结束使用时间作为key,每次从堆顶返回最早结束使用的牛棚。这里如果不用优先队列也可以自己写一个堆。博主就用STL的priority_queue了。

以上,我们就能写出完整的代码。整体思路如下:

将奶牛根据挤奶开始时间排序后,从第一只奶牛进行考虑。从牛棚的优先队列中返回结束时间最短的牛棚,看该奶牛是否能放在该牛棚中。如果能,就更新该牛棚的结束时间,更新为该奶牛的开始时间;如果不能,就新创建一个牛棚,并将奶牛放在这个牛棚中。重复以上,直到所有奶牛都被放完。

这样,我们需要2个辅助数据。int pos[50010]。因为奶牛与牛棚都有编号,我们就用该数组存放第i头奶牛放在第k个牛棚中。int stall_num。每创建一个新牛棚就自加1,不仅能给牛棚编号,最终能标记总共有多少个牛棚。

C++ 代码 720ms

#include<iostream>
#include<vector>
#include<string>
#include<cmath>
#include<algorithm>
#include<map>
#include<utility>
#include<queue>
#define TIME std::ios::sync_with_stdio(false)
#define LL long long
#define MAX 310
#define INF 0x3f3f3f3f

using namespace std;

struct cow{
    int begin,end;
    int no;
    bool operator<(const cow &c) const{
        return begin < c.begin;
    }
}cows[50010];
int pos[50010];

struct stall{
    int end;
    int no;
    bool operator<(const stall &s) const{
        return end > s.end;
    }
};

int main() {
    TIME;
    int N;
    while(cin >> N){
        for(int i = 0;i < N;i++){
            cin >> cows[i].begin >> cows[i].end;
            cows[i].no = i;
        }
        sort(cows,cows+N);
        int stall_num = 0;
        priority_queue<stall> pq;
        for(int i = 0;i < N;i++){
            if(pq.empty()){
                stall_num++;
                stall temp;
                temp.end = cows[i].end;
                temp.no = stall_num;
                pq.push(temp);
                pos[cows[i].no] = stall_num;
            }else{
                stall empty_stall = pq.top();
                if(empty_stall.end < cows[i].begin){
                    pq.pop();
                    pos[cows[i].no] = empty_stall.no;
                    empty_stall.end = cows[i].end;
                    pq.push(empty_stall);
                }else{
                    stall_num++;
                    stall temp;
                    temp.end = cows[i].end;
                    temp.no = stall_num;
                    pq.push(temp);
                    pos[cows[i].no] = stall_num;
                }
            }
        }
        cout << stall_num << endl;
        for(int i = 0;i < N;i++){
            cout << pos[i] << endl;
        }
    }

    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值