POJ 3190 Stall Reservations (priority queue structure +)

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.
 
Meaning of the title: the title means that each barn while milking a cow only on the inside, every cow has a start and end time.
Q. How much do you want to make the most of all the cattle barn finished milking, there is a detail of the end of time is equal to the start time if the cow into the barn on the cow,
This calculation intersection, can not be put together.
Ideas: a greedy title, with priority queuing time for each end of the maintenance barn, end time early in the first team, first-out team. If there are not cattle into the barn
Start time is greater than the end of this time, then put the team out of the end time, end time these cows into the team.
Considering the topics to be output per cow by serial number, which came into the barn, then we have to record what the barn, and finally press the ordinal sort output .
 
 
  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 }

 

 

Guess you like

Origin www.cnblogs.com/xwl3109377858/p/11330716.html