Poj 3190 stall reservations (Greedy)

Source: Internet
Author: User

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 .. B (1 <= A <= B <= 1,000,000), where des both times A and B. obviusly, 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 stils required in the barn so that each cow can have her private milking Period
  • An assignment of cows to these stils over time
Specified 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 stallthe 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 10 
Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>
Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..
Stall 3 .. .. c3>>>>>>>>> .. .. .. ..
Stall 4 .. .. .. c5>>>>>>>>> .. .. ..
Other outputs using the same number of stallare possible. Question: a cow needs to be milked at a specified time. One machine can only milk one cow and the minimum number of machines is required. Train of Thought: Use the priority queue in STL to determine whether the idle machine meets the milking time of the next cow. If it is satisfied, let the cows that have been squeezed out of the queue and number the machine to the next cow, the next cow enters the team. Otherwise, machine + 1 and the next cow enters the team. note: The output sequence is the same as the input sequence. When the input is performed, the subscript is stored in the struct. When an array is opened to record the machine number, the subscript will not be messy after sorting. AC code:
 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<iostream> 5 #include<queue> 6 using namespace std; 7 struct node 8 { 9     int a;10     int b;11     int c;12     bool operator<(const node &c)const13     {14         if(b==c.b)15             return a>c.a;16         return b>c.b;17     }18 } d[60000];19 priority_queue<node> q;20 int s[60000];21 bool cmp(struct node a,struct node b)22 {23     if(a.a==b.a)24         return a.b<b.b;25     return a.a<b.a;26 }27 int main()28 {29     int a,b,i,j,n=1;30     scanf("%d",&a);31     for(i=1; i<=a; i++)32     {33         scanf("%d%d",&d[i].a,&d[i].b);34         d[i].c=i;35     }36     sort(d+1,d+a+1,cmp);37     q.push(d[1]);38     s[d[1].c]=1;39     for(i=2; i<=a; i++)40     {41         int x=q.top().b;42         if(x<d[i].a)43         {44             s[d[i].c]=s[q.top().c];45             q.pop();46         }47         else48         {49             n++;50             s[d[i].c]=n;51         }52         q.push(d[i]);53     }54     printf("%d\n",n);55     for(i=1; i<=a; i++)56         printf("%d\n",s[i]);57 }

 

If you have any questions, please comment on them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.