码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3190 Stall Reservations

时间:2018-03-22 12:39:55      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:interval   队列   you   imu   his   pst   Plan   rate   ted   

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.
 
 一群奶牛,只在固定的时间产奶,每头牛只在固定的时间产奶,每头牛都需要一个挤奶的机器
 所有奶牛产奶,最少需要多少个机器,输出每一个奶牛使用挤奶机器的编号
 
 
  这题贪心 ,需要用优先队列优化 , 
 
 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 }

 

 

POJ 3190 Stall Reservations

标签:interval   队列   you   imu   his   pst   Plan   rate   ted   

原文地址:https://www.cnblogs.com/qldabiaoge/p/8622621.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!