bzoj1620[usaco2008 Nov]time Management Time Management
Test instructions
n tasks, each with a required time and the latest finish time, ask when to start working at the latest. n≤1000
Exercises
Greedy, according to the latest finish time from morning to night sort, if the current task too late to complete, will be ahead of the task forward, or accumulate a "free time." When pushing a task, if there is "free time" before, use free time to reduce the time to push forward, otherwise use the latest start time to reduce the time to push forward. Reflection: I began to be greedy and wrong, at the latest start of the day from morning to night sort, the result WA long. It is still not clear why, I hope that God Ben can help me point out.
The following data will be faulted:
2
10 20
1 12
If the correct output should be 9, but according to my wrong solution output is 1.
Code:
1#include <cstdio>2#include <algorithm>3#include <cstring>4 #defineInc (I,J,K) for (int i=j;i<=k;i++)5 #defineMAXN 11006 using namespacestd;7 8InlineintRead () {9 CharCh=getchar ();intf=1, x=0;Ten while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=GetChar ();} One while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); A returnf*x; - } - intNstructjob{intt,s;}; Job JOBS[MAXN]; the BOOLCMP (Job A,job b) {returna.s==b.s?a.t<b.t:a.s<B.S;}; - intMain () { -N=read (); Inc (I,1, n) jobs[i].t=read (), Jobs[i].s=read (); Sort (jobs+1, jobs+1+n,cmp); - intans=jobs[1].s-jobs[1].t,rem=0; +Inc (I,2, N) { - if(jobs[i].s-jobs[i].t<jobs[i-1].S) rem-=jobs[i-1].s-(jobs[i].s-jobs[i].t); + Elserem+=jobs[i].s-jobs[i].t-jobs[i-1].s; A if(rem<0) ans+=rem,rem=0;if(ans<0) {printf ("-1");return 0;} at } -printf"%d", ans);return 0; -}
20160729
bzoj1620[usaco2008 nov]time Management Time Management *