http://poj.org/problem?id=3190
Stall Reservations
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 3567 |
|
Accepted: 1276 |
|
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.
Source
Usaco 2006 February Silver
Analysis:
The problem is that some cows have to squeeze milk at a specified time, and a machine can only work on a cow at the same moment. Give you the interval of the specified time for each cow and ask how many machines you need minimum.
The first thing I wanted to do was sort from small to large at the point where the cows demanded time, but then found that the idea was wrong.
1 6 2 8 8 3 4 8 4 6 Ten 5 1 3 6 2 4 7 4 7
After adjustment, you should first sort from small to large according to the time starting point of the cows ' requirements, then maintain a priority queue,
The end time for cows that have already started milking is a priority. Then each time only need to check whether there is currently a cow milking work has been completed machine, if any, then change the machine to work. If not, add a new machine.
AC Code:
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <queue>6 using namespacestd;7 Const intmaxn=60000;8 intN,USE[MAXN];9 structNodeTen { One intl; A intR; - intPos; - BOOL operator< (ConstNode &a)Const the { - if(r==A.R) - returnL>A.L; - returnR>A.R; + } - }A[MAXN]; +Priority_queue<node>Q; A BOOLCMP (Node a,node B) at { - if(a.l==B.L) - returna.r<B.R; - returna.l<B.L; - } - intMain () in { - while(SCANF ("%d", &n)! =EOF) to { + for(intI=0; i<n;i++) - { thescanf"%d%d",&a[i].l,&A[I].R); *a[i].pos=i; $ }Panax NotoginsengSort (a,a+n,cmp); -Q.push (a[0]); the intnow=0, ans=1; +use[a[0].pos]=1; A for(intI=1; i<n;i++) the { + if(!q.empty () &&q.top () .r<a[i].l) - { $use[a[i].pos]=use[q.top (). Pos]; $ Q.pop (); - } - Else the { -ans++;Wuyiuse[a[i].pos]=ans; the } - Q.push (A[i]); Wu } -printf"%d\n", ans); About for(intI=0; i<n;i++) $printf"%d\n", Use[i]); - while(!q.empty ()) - Q.pop (); - } A return 0; +}
AC Code:
1#include <iostream>2#include <algorithm>3#include <cstring>4#include <queue>5#include <cstdio>6 using namespacestd;7 structTT8 {9 intx, y, id;Ten}a[50010]; One intvis[50010]; A BOOLCMP (TT m, tt N) - { - if((m.x<n.x) | | (m.x==n.x && M.Y<N.Y))return true; the return false; - } - BOOL operator<(TT A, TT b) { - returnA.y>b.y; + } - intMain () + { A intt,i; at while(~SCANF ("%d",&T)) - { -Priority_queue<tt>pp; -memset (Vis,0,sizeof(Vis)); - for(i=1; i<=t;i++) - { inscanf"%d%d",&a[i].x,&a[i].y); -A[i].id =i; to } + TT now; -Sort (A +1, A +1+t,cmp); the intAns =1; *vis[a[1].id]=ans; $Pp.push (a[1]);Panax Notoginseng for(i=2; i<=t; i++) - { thenow =pp.top (); + if(a[i].x>now.y) A { theVis[a[i].id] =Vis[now.id]; +Pp.pop ();//remove the point; -Now.y = A[I].Y;//update the end point; $ Pp.push (now); $ } - Else - { theVis[a[i].id] = + +ans; - Pp.push (A[i]);Wuyi } the } -printf"%d\n", ans); Wu for(intI=1; i<=t;i++) - { Aboutprintf"%d\n", Vis[i]); $ } - } - return 0; -}
POJ 3190 Stall Reservations