Schedule a shift
The main topic: There are n cows, they are responsible for the time of the T-time, at least one on duty, each cow has a period of free time can be on duty, to meet the requirements of the minimum number of cows, can not meet the output-1.
Analysis:
The cow idle time is considered as a line segment, sorted by the left end of the segment, sorted from the first segment, each time a segment with coincident or exactly connected to the previous segment is selected, and at the same time select the largest right endpoint, let the cow on duty. To avoid selecting the right endpoint of a segment that is smaller or equal than the right endpoint of the previous selected segment, be careful to handle the first and last segments.
Code:
ProgramCleaning;varB:Array[0..25000] ofLongint; N,i,m,j,k,t,g,p:longint;procedureqsort (l,h:longint);varI,j,t,m:longint;beginI:=l; j:=h; M:=a[(I+J)Div 2]; Repeat whileA[i]<m DoInc (i); whileM<A[J] DoDec (j);ifI<=j Then beginT:=a[i]; A[I]:=A[J]; a[j]:=t; T:=b[i]; B[I]:=B[J]; b[j]:=T; Inc (i); Dec (j); End;untilI>J;ifI ThenQsort (I,H);ifJ>l ThenQsort (L,J);End;beginassign (input,'cleaning.in'); Reset (input); Assign (output,'Cleaning.out'); Rewrite (output); READLN (n,t); fori:=1 toN Doread (a[i],b[i]); Qsort (1, N); K:=1; fori:=1 toN Do begin ifA[i]<=k Then if(b[i]>m) and(b[i]>=p) Thenm:=B[i]; ifA[i]>k Then ifa[i]>m+1 Then beginWriteln (-1); BreakEnd Else begink:=m+1;p: =k; j:=j+1;ifM>=t Then beging:=1; BreakEnd; M:=b[i];End; End; ifM>=t Then ifg=0 ThenWriteln (j+1)ElseWriteln (j)Else ifI=n ThenWriteln (-1); Close (input); Close (output);End.
View Code
Usaco: Arrange on duty s2004dec (greedy)