Greedy method, the time of the execution of the task J longer should be confessed first. The correctness can be proved by the adjacent Exchange method. In fact, for two people, to make the total time is the shortest, it is necessary to do two things at the same time the longest time.
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<cmath>#include<map>#include<Set>#include<vector>#include<algorithm>#include<stack>#include<queue>#include<cctype>#include<sstream>using namespacestd;#definePII pair<int,int>#defineLL Long Long intConst inteps=1e-8;Const intinf=1000000000;Const intmaxn= ++Ten;structnode{intB,j;} A[MAXN];intn,ans,cas=0;BOOLCMP (node X,node y) {returnX.j>Y.J;}intMain () {//freopen ("In2.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout); while(SCANF ("%d", &n) = =1&&N) { for(intI=0; i<n;i++) {scanf ("%d%d", &a[i]. b,&A[i]. J); } sort (A,a+n,cmp); Ans=a[0]. b+a[0]. J intt=a[0]. b+a[0]. J for(intI=1; i<n;i++) {T+=a[i]. B+a[i]. j-a[i-1]. J//T indicates the time of each "horizontal bar"ans=Max (ans,t); } printf ("Case %d:%d\n",++Cas,ans); } //fclose (stdin); //fclose (stdout); return 0;}
Uva11729-commando War (greedy)