Stall Reservations
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 7008 | Accepted: 2519 | 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:
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
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.
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.
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:
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.
题意:
有N头奶牛,(1<=N<=50000),每头奶牛只在特定的时间段内[A,B]内才愿意挤奶(1<=A,B<=1000000)。
一个挤奶机只能同时挤一头奶牛。农夫约翰最少需要准备多少个挤奶机,才能满足所有奶牛的挤奶需求。
这题的贪心其实很容易会想到要去找冲突矛盾最多的就是机器数,
(整体按左端点从小到大排序)
然后问题就是每只奶牛的分配了,优先队列的用处就在于将右端点从小到大排序,
以保证最先空出来的尽量能够被利用
我的朋友还有另一种解法,(虽然据说是在午睡梦中想出来的),但是很显然也是可行的,
将端点全部单独取出,标记是左还是右,从小到大排序(相同时左端点在前),
然后用普通队列来存放空出的机器,之后扫一遍,
其实原理差不多,都保证了机器能被最大化利用,等他有空了我去扒扒他的代码再发过来
(嗯我是奸商)
Code:
Status | Accepted |
---|---|
Time | 391ms |
Memory | 2036kB |
Length | 1473 |
Lang | G++ |
Submitted | 2017-07-14 09:43:40 |
Shared |
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int Max = 50000;
struct node{
int l, r;
int pos;
bool operator < (const node & X) const{
if(l == X. l) return r < X.r;
return l < X.l;
}
bool operator > (const node & X) const{
//if(r == X.r) return l > X.l;
return r > X. r;
}
}Cow[Max + 5];
int N;
int Room[Max + 5];
priority_queue<node, vector<node>, greater<node> >Q;
bool getint(int & num){
char c; int flg = 1; num = 0;
while((c = getchar()) < '0' || c > '9'){
if(c == '-') flg = -1;
if(c == -1) return 0;
}
while(c >= '0' && c <= '9'){
num = num * 10 + c - 48;
if((c = getchar()) == -1) return 0;
}
num *= flg;
return 1;
}
int main(){
while(getint(N)){
for(int i = 1; i <= N; ++ i)
getint(Cow[i].l), getint(Cow[i].r), Cow[i].pos = i;
sort(Cow + 1, Cow + 1 + N);
int R = 1;
Q.push(Cow[1]);
Room[Cow[1].pos] = R;
for(int i = 2; i <= N; ++ i){
if(! Q.empty() && Q.top().r < Cow[i].l){
Room[Cow[i].pos] = Room[Q.top().pos];
Q.pop();
Q.push(Cow[i]);
}
else {
++ R;
Room[Cow[i].pos] = R;
Q.push(Cow[i]);
}
}
printf("%d\n", R);
for(int i = 1; i <= N; ++ i)
printf("%d\n", Room[i]);
while(! Q.empty()) Q.pop();
}
return 0;
}