Describe
http://www.lydsy.com/JudgeOnline/problem.php?id=1620
n work, each work it takes time, and complete deadline, ask to complete all work, at the latest when to start.
Analysis
We can think of the time of the two-minute start. For a given time, determine whether it can be done.
How to judge it?
We assume that there are \ (a,b\) two tasks and \ (Deadline (a) <deadline (b) \).
If \ (b\) is completed first, then \ (Time[b]+time[a]<deadline (a) \) is required.
If you finish \ (a\) First, then ask \ (Time[a]<deadline (a), time[a]+time[b]<deadline (b) \).
Obviously the second scenario is more likely to be completed, so our greedy choice is to complete the small task first (deadline\) at a time.
1#include <bits/stdc++.h>2 #defineFST First3 #defineSCD second4 using namespacestd;5 6typedef pair <int,int>P;7 Const intmaxn= ++5;8 intn,max=~0u>>1;9 P A[MAXN];TenInlineintReadint&X) {x=0;intk=1;CharC for(C=getchar ();c<'0'|| C>'9'; C=getchar ())if(c=='-') k=-1; for(; c>='0'&&c<='9'; C=getchar ()) x=x*Ten+c-'0';returnx*=K;} OneInlineBOOLCMP (P a,p b) {returna.scd<B.SCD;} AInlineBOOLCintx) { - for(intI=1, t=x-1; i<=n;i++){ -t+=a[i].fst; the if(T>=A[I].SCD)return false; - } - return true; - } +InlineintBsearch (intLintR) { - while(l<R) { + intmid=l+ (r-l+1)/2; A if(C (mid)) l=mid; at Elser=mid-1; - } - returnl; - } -Inlinevoidinit () { - read (n); in for(intI=1; i<=n;i++) Read (A[I].FST), read (A[I].SCD), Max=min (max,a[i].scd-a[i].fst); -Sort (A +1, a+n+1, CMP); to } + intMain () { - init (); the intAns=bsearch (0, Max); * if(ans==0) ans=-1; $printf"%d\n", ans);Panax Notoginseng return 0; -}
View Code
Bzoj_1620_[usaco2008_nov]_time_management_ Time Management _ (two points + greedy)