http://poj.org/problem?id=3190
The original idea is sorted by array also good heap maintenance is want to let start time > End time of the cattle merge and then finally find out the number of good
But it doesn't get the distribution of every cow.
So the simulation and greedy heap simulation is milking the machine by the end time from small to large arrangement no search added a cow on the end of a cow milking
This can be used to find out which machine each cow is milking and if the cow is finished by the time it is added to the cow, then it will be ready to use or it can only be added to a new machine.
And then stall is entering a cow to make sure a stall is a pure simulation of this process.
A good problem of greed and imitation
1#include <iostream>2#include <stdio.h>3#include <algorithm>4#include <queue>5 6 using namespacestd;7 8 intN;9 intstall[50003];Ten struct Section One { A intx, y; - intPos; - BOOL operator< (section A)Const //Heap by end time in ascending order the { - if(y = = a.y)returnX >a.x; - Else returnY >a.y; - } + -}section[50003]; + A BOOLCMP (sections A, section B)//array sorted by start time in ascending order at { - if(a.x = = b.x)returnA.y <b.y; - Else returnA.x <b.x; - } - -Priority_queue<section>que; in intSolve () - { to intAns =0; + Section node; - for(inti =0; I < N;i + +) the { * if( !que.empty ()) $ {Panax Notoginsengnode =que.top (); - if(Que.top (). Y < section[i].x)//If you add this section to the end of the cow-->>> here is wrong not when the end time and the start time are equal to not be replaced the { +Stall[section[i].pos] =stall[que.top (). Pos]; AQue.pop ();//This cow's going to be out. the } + Else - { $ans++; $Stall[section[i].pos] =ans; - } - } the Else - {Wuyians++; theStall[section[i].pos] =ans; - } Wu Que.push (Section[i]); - } About returnans; $ } - - intMain () - { AFreopen ("In.txt","R", stdin); + while(~SCANF ("%d", &N)) the { - while(!que.empty ()) Que.pop (); $ for(inti =0; i < N; i++) the { thescanf"%d%d", §ion[i].x, §ion[i].y); theSection[i].pos =i; the //Stall[section[i].pos] = i;//arrange the initial stall - } inSort (section, section+n, CMP);//When you customize a comparison function, you can ignore operator overloads inside the struct the intAns =solve (); theprintf"%d\n", ans); About for(inti =0; i < N; i++) the { theprintf"%d\n", Stall[i]); the } + } - return 0; the}
POJ 3190 Stall Reservations