POJ 3190 Stall Reservations
Description
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
Lines 2..N+1: Line i+1 describes cow i’s milking interval with two space-separated integers.
Output
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
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.
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<algorithm> 5 using namespace std; 6 const int maxn=50010; 7 struct node { 8 int x,y,id; 9 friend bool operator<(node a,node b) { 10 return a.y>b.y; 11 } 12 } qu[maxn]; 13 int cmp(node a,node b) { 14 return a.x<b.x; 15 } 16 int ID[maxn]; 17 int main() { 18 int n; 19 while(scanf("%d",&n)!=EOF ) { 20 for (int i=0 ; i<n ; i++) { 21 scanf("%d%d",&qu[i].x,&qu[i].y); 22 qu[i].id=i; 23 } 24 sort(qu,qu+n,cmp); 25 memset(ID,0,sizeof(ID)); 26 priority_queue<node>q; 27 int ans=1; 28 q.push(qu[0]); 29 ID[qu[0].id]=1; 30 for (int i=1 ; i<n ; i++) { 31 if (q.top().y<qu[i].x) { 32 ID[qu[i].id]=ID[q.top().id]; 33 q.pop(); 34 q.push(qu[i]); 35 } else { 36 ID[qu[i].id]=++ans; 37 q.push(qu[i]); 38 } 39 } 40 printf("%d\n",ans); 41 for (int i=0 ; i<n ; i++) { 42 printf("%d\n",ID[i]); 43 } 44 } 45 return 0; 46 }