Usaco: Arrange on duty s2004dec (greedy)

Source: Internet
Author: User

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&LT;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)

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.