POJ 3190 Stall Reservations (优先队列+结构体)

Stall Reservations

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<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<algorithm>
  6 #include<map>
  7 #include<set>
  8 #include<vector>
  9 #include<queue>
 10 #include<list>
 11 #include<stack>
 12 //#include<unordered_map>
 13 using namespace std;
 14 #define ll long long 
 15 #define dd cout<<endl
 16 const int mod=1e9+7;
 17 const int nf=1e9+7;
 18 
 19 const int maxn=5e4+10;
 20 
 21 typedef struct
 22 {
 23     int a;
 24     int b;
 25     int number;
 26 } St;
 27 //优先队列+结构体,定义优先级 
 28 bool operator<(const St &x,const St &y)
 29 {
 30     return x.b > y.b;
 31  } 
 32  
 33 bool cmp(const St &x,const St &y)
 34 {
 35     if(x.a != y.a)
 36         return x.a < y.a;
 37     else
 38         return x.b < y.b;
 39 }
 40  
 41 St sum[maxn];
 42 
 43 int main()
 44 {
 45     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 46     
 47     int n;
 48     cin>>n;
 49     
 50     int a,b;
 51     
 52     priority_queue<St>q;    
 53     
 54     int ans=0;
 55     vector<St>v;
 56 
 57     for(int i=0;i<n;i++)
 58     {
 59         cin>>sum[i].a>>sum[i].b;
 60         sum[i].number=i;
 61     }
 62     
 63     sort(sum,sum+n,cmp);//这里先根据开始顺序排下序 
 64     
 65     for(int i=0;i<n;i++)
 66     {
 67         a=sum[i].a;//当前开始时间 
 68         b=sum[i].b;//当前结束时间 
 69         
 70         if(q.empty())//空的直接进篱笆 
 71         {
 72             ans++;
 73             q.push({ans,b});
 74             v.push_back({sum[i].number,ans});//记录 
 75         }
 76         else
 77         {
 78             St now=q.top();
 79             
 80             if(now.b < a)//当前开始时间大于最早的结束时间 
 81             {
 82                 q.pop();
 83                 
 84                 v.push_back({sum[i].number,now.a});//记录 
 85                 q.push({now.a,b});
 86             }
 87             else//在进一个篱笆 
 88             {
 89                 ans++;
 90                 q.push({ans,b});
 91                 v.push_back({sum[i].number,ans});//记录 
 92             }
 93         }    
 94     }
 95     
 96     cout<<ans<<endl;//篱笆数 
 97     
 98     sort(v.begin(),v.end(),cmp);//按奶牛序号排序 
 99     
100     for(int i=0;i<v.size();i++)//输出答案 
101         cout<<v[i].b<<endl;
102     
103     return 0;
104 }

猜你喜欢

转载自www.cnblogs.com/xwl3109377858/p/11330716.html