POJ 3190 Stall Reservations

来源:互联网 发布:临沂淘宝客服招聘信息 编辑:程序博客网 时间:2024/06/18 08:03

                                                                                                                 Stall Reservations
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7525 Accepted: 2673 Special Judge

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

51 102 43 65 84 7

Sample Output

412324

Hint

Explanation of the sample:

Here's a graphical schedule for this output:

Time     1  2  3  4  5  6  7  8  9 10Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..Stall 3 .. .. c3>>>>>>>>> .. .. .. ..Stall 4 .. .. .. c5>>>>>>>>> .. .. ..
Other outputs using the same number of stalls are possible.
题目大意:一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作。给你每头奶牛的指定时间的区间,问你最小需要多少机器。

贪心+优先队列

怎么贪心呢?我们可以这样:先建一个结构体,再排一下序,开始时间早的排在前面,如果开始时间相同,则结束时间早的排在前面。将第一个放进队列里,循环,若有一个的开始时间大于队首的结束时间,则他们可以用同一台机器

#include<iostream>#include<queue>#include<algorithm>#include<cstdio>using namespace std;struct Node{int start,end,pos;bool operator < (const Node &a)const{return end>a.end;}}array[50010];bool cmp(Node x,Node y){if(x.start==y.start) return x.end<y.end;return x.start<y.start;}int main(){int use[50010];priority_queue<Node> que;int n;cin>>n;for(int i=0;i<n;i++){scanf("%d%d",&array[i].start,&array[i].end);array[i].pos=i;}sort(array,array+n,cmp);que.push(array[0]);use[array[0].pos]=1;int kount=1;for(int i=1;i<n;i++){if(!que.empty()&&que.top().end<array[i].start){use[array[i].pos]=use[que.top().pos];que.pop();}else{kount++;use[array[i].pos]=kount;}que.push(array[i]);}cout<<kount<<endl;for(int i=0;i<n;i++) printf("%d\n",use[i]);}


#include<iostream>#include<queue>#include<algorithm>#include<cstdio>using namespace std;struct Node{int start,end,pos;bool operator < (const Node &a)const{return end>a.end;}}array[50010];bool cmp(Node x,Node y){if(x.start==y.start) return x.end<y.end;return x.start<y.start;}int main(){int use[50010];priority_queue<Node> que;int n;cin>>n;for(int i=0;i<n;i++){scanf("%d%d",&array[i].start,&array[i].end);array[i].pos=i;}sort(array,array+n,cmp);que.push(array[0]);use[array[0].pos]=1;int kount=1;for(int i=1;i<n;i++){if(!que.empty()&&que.top().end<array[i].start){use[array[i].pos]=use[que.top().pos];que.pop();}else{kount++;use[array[i].pos]=kount;}que.push(array[i]);}cout<<kount<<endl;for(int i=0;i<n;i++) printf("%d\n",use[i]);}


原创粉丝点击