Stall Reservations POJ - 3190(贪心)
Posted iwannabe
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Stall Reservations POJ - 3190(贪心)相关的知识,希望对你有一定的参考价值。
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:
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
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.
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.
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:
Here‘s a graphical schedule for this output:
Time 1 2 3 4 5 6 7 8 9 10Other outputs using the same number of stalls are possible.
Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>
Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..
Stall 3 .. .. c3>>>>>>>>> .. .. .. ..
Stall 4 .. .. .. c5>>>>>>>>> .. .. ..
题意:N头牛,每头牛都有霸占食槽的习惯,从【cow【i】.l,cow【i】.r】,被霸占的食槽,无法给其他牛使用,就必须多几个平行食槽
问最少几个平行食槽可供牛使用,且输出每个牛所在食槽处。
思路:一个食槽,能否放入下一个牛,取决于前一个牛的cow【i-1】.r 是否小于 cow【i】.l
我们可以想到,如果cow【i-1】.r >= cow【i】.l , 就需要另外开一行食槽。
cow【i-1】.r < cow【i】.l 那我们就可以把当前的cow【i】放到这一行食槽中。
但是这样不一定是最优的,你虽让能放进去,但是前面牛的越早结束越好。
这样的话就可以用一个最小堆维护每列牛最右边的食槽,
开始我想到这,以为有多少食槽就要建立多少个堆,让后再去找它符合哪个队(显然这样不行),其实只用一个堆就可以了,
一个堆的话如果右边界最小的你都不满足,那么其他的你肯定不满足
我们还需要事前将牛按照左边界排序,因为对于同一个最小的右边界,我们肯定希望塞入左边界最小的牛
1 #include<cstdio> 2 #include<iostream> 3 #include<queue> 4 #include<algorithm> 5 using namespace std; 6 7 const int maxn = 5e4+5; 8 int n; 9 struct Node 10 { 11 int id; 12 int l,r; 13 }; 14 15 int mp[maxn]; 16 Node cow[maxn]; 17 bool cmp1(Node a,Node b) 18 { 19 return a.l < b.l; 20 } 21 22 bool operator<(Node a,Node b) 23 { 24 return a.r > b.r; 25 } 26 27 priority_queue<Node>que; 28 int main() 29 { 30 scanf("%d",&n); 31 for(int i=1; i<=n; i++) 32 { 33 scanf("%d%d",&cow[i].l,&cow[i].r); 34 cow[i].id = i; 35 } 36 sort(cow+1,cow+1+n,cmp1); 37 int k = 1; 38 for(int i=1; i<=n; i++) 39 { 40 if(que.empty()) 41 { 42 mp[cow[i].id] = k; 43 } 44 else if(que.top().r < cow[i].l) 45 { 46 47 mp[cow[i].id] = mp[que.top().id]; 48 que.pop(); 49 } 50 else 51 mp[cow[i].id] = ++k; 52 que.push(cow[i]); 53 } 54 printf("%d ",k); 55 for(int i=1; i<=n; i++) 56 { 57 printf("%d ",mp[i]); 58 } 59 }
以上是关于Stall Reservations POJ - 3190(贪心)的主要内容,如果未能解决你的问题,请参考以下文章
POJ--3176 Stall Reservations(DP)