Stall Reservations
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 7646 |
|
Accepted: 2710 |
|
Special Judge |
Description
Oh those picky n (1 <= n <= 50,000) cows! They is so picky, each one is only being 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 t o determine which stall each cow can is assigned for her milking time. Of course, no cow would share such a private moment with other cows.
Help FJ by determining:
- The minimum number of stalls required in the barn so, each cow can has her private milking period
- An assignment of cows to these stalls over time
Many answers is correct for each test dataset, a program would grade your answer.
Input
Line 1: A single integer, N
Lines 2..n+1:line i+1 describes cow i ' s milking interval with the space-separated integers.
Output
Line 1:the minimum number of stalls the barn must has.
Lines 2..n+1:line i+1 describes the stall to which cow I 'll be a assigned for her milking period.
Sample Input
51 102 43 65 84 7
Sample Output
412324
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 is possible.This water problem really pit, many people think is greedy, knapsack problem. Even teammates have discussed the tree-like array during the race .... The game is over Peng elder brother said C question is water problem, E priority queue. Meow meow meow??? think about it. Didn't even think about it!! every time I read the puzzle is so simple how not to come out, the tolerance of sadness ... This problem modifies the priority of the queue because the current R value must be less than the L value for the next cow in the same slot. So the priority R value takes precedence.
so the crux of the problem is, how to deal with the current slot, how to construct the queue priority!!
#include <iostream> #include <cstdio> #include <queue> #include <algorithm>using namespace std; struct node{int l,r,n; BOOL operator < (const node &b) const {return r>b.r| | (R==B.R&&L>B.L); }}A[50005];p riority_queue <node> q;int u[50005];bool cmp (node A,node b) {return a.l<b.l| | (A.L==B.L&&A.R<B.R);} int main () {int n,m; while (~SCANF ("%d", &n)) {int Ans=1; for (int i=1;i<=n;i++) {scanf ("%d%d", &A[I].L,&A[I].R); A[i].n=i; } sort (a+1,a+n+1,cmp); Q.push (a[1]); U[a[1].n]=1; for (int i=2;i<=n;i++) {if (!q.empty () &&q.top (). r<a[i].l) {U[a[i].n]=u[q.top (). n]; Q.pop (); } else{ans++; U[a[i].n]=ans; } q.push (A[i]); } printf ("%d\n", ans); for (int i =1;i<=n;i++) { printf ("%d\n", U[i]); } while (!q.empty ())//is particularly important, emptying the queue q.pop (); }}
POJ 3190 Stall Reservations (priority queue) C + +