Cow Milking
The main problem: a group of cows are very picky, they only in a time period of milking, and only in a shed inside, can not be shared with other cows, and now give you a herd of cows, ask you if you want all cows milking, at least how many cattle barn?
This topic if the time zone is removed, it becomes the box problem (loaded box to use splay maintenance), but now specify the interval and time, we only use greedy algorithm can be, each time only use to find the smallest can be (with heap maintenance).
PS: At first I thought of using AVL to maintain, do not know what I think, it is a waste of time
1#include <iostream>2#include <functional>3#include <algorithm>4 5 using namespacestd;6 7typedefstructIterval8 {9 intCows_num;Ten intWhich_stall; One intstart; A intend; - }cow; -typedefstructBox the { - intBox_num; - intmin_t; - }stall; +typedefintPosition; - intFCOMP1 (Const void*a,Const void*b) + { A return(* (* (COW *) a). Start-(* (Cow *) (b). Start; at } - intFCOMP2 (Const void*a,Const void*b) - { - return(* (* (COW *) a). Cows_num-(* (Cow *) b). Cows_num; - } - in StaticCow cows_set[50000]; - StaticStall s_heap[50001]; to Static intSum_of_stalls; + - voidSearch (Const int); the voidInsert (Stall, Position); *Stall Delete_min (void); $ Panax Notoginseng intMainvoid)//This problem is maintained with a heap - { the intN; + while(~SCANF ("%d", &N)) A { the for(inti =0; I < n; i++) + { -scanf"%d%d", &cows_set[i].start, &cows_set[i].end); $Cows_set[i].cows_num =i; $ } -Qsort (Cows_set, N,sizeof(Cow), FCOMP1); - Search (n); the } - return 0;Wuyi } the - voidInsert (Stall goal, Position POS) Wu { -Position s =POS, PR; About $ for(; s >1; s = PR)//Upper Filter - { -PR = s%2==0? S >>1: (S-1) >>1; - if(S_heap[pr].min_t >goal.min_t) AS_heap[s] =S_HEAP[PR]; + Else Break; the } -S_heap[s] =goal; $ } the theStall Delete_min (void) the { theStall mins_stalls = s_heap[1],tmp = s_heap[sum_of_stalls--]; - Position PR, s1, S2; in the for(pr =1; PR <=sum_of_stalls;) the { AboutS1 = PR <<1; S2 = S1 +1; the if(S2 <=sum_of_stalls) the { the if(S_heap[s1].min_t <s_heap[s2].min_t) { +S_HEAP[PR] = s_heap[s1]; PR =S1; - } the Else{BayiS_HEAP[PR] = S_heap[s2]; PR =S2; the } the } - Else - { the if(S1 <=sum_of_stalls) { theS_HEAP[PR] = s_heap[s1]; PR =S1; the } the Break; - } the } the Insert (TMP, PR); the returnMins_stalls;94 } the the voidSearch (Const intN) the {98 Stall tmp; AboutSum_of_stalls =1; -Tmp.box_num =1; tmp.min_t = cows_set[0].end;101Insert (TMP,1);102cows_set[0]. Which_stall =1; 103 104 for(inti =1; I < n; i++) the {106 if(Cows_set[i].start <= s_heap[1].min_t)//Not fit107 {108Tmp.box_num = ++sum_of_stalls; tmp.min_t =Cows_set[i].end;109 Insert (TMP, sum_of_stalls); theCows_set[i]. Which_stall =Sum_of_stalls;111 } the Else113 { theTMP =delete_min (); thetmp.min_t =Cows_set[i].end; theCows_set[i]. Which_stall =Tmp.box_num;117Insert (TMP, + +)sum_of_stalls);118 }119 } -printf"%d\n", sum_of_stalls);121Qsort (Cows_set, N,sizeof(Cow), FCOMP2);122 for(inti =0; I < n; i++)123printf"%d\n", Cows_set[i]. Which_stall);124}
Greedy:stall Reservations (POJ 3190)