bzoj3062[usaco2013 Feb]taxi
Test instructions
Bessie on the farm to provide taxi services to other cows, she must get to the start of these cows and take them to their destination. Bessie's car was so small that she could only carry one cow at a time. n the starting position and end of the cows are known, please determine the minimum travel Bessie from 0 to the M point. Bessie realized that to make the resulting trip the shortest, Bessie may be on the way to let the cows get on or off the train and not necessarily a cow from the starting point directly to the end. n≤100000,m≤1000000000.The following:God is greedy ... First of all, the distance from the beginning to the end of each cow is unavoidable, so first count them into the answer, then the goal is to minimize the empty time. Then it will be found that the optimal strategy is to carry a cow in the process just meet the cow to ride, put the current cow to download this, and then back to the place (this process is we want to minimize the "empty" stage) was put down before the cows (note that there may be many cows were put down in the process). Therefore, in the beginning of the array to add m, the end array to add 0, sorting them, and then the answer in turn to accumulate ABS (A[i]-b[i]).Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <queue>5 #defineInc (I,J,K) for (int i=j;i<=k;i++)6 #defineMAXN 1000107 using namespacestd;8 9InlineintRead () {Ten CharCh=getchar ();intf=1, x=0; One while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=GetChar ();} A while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); - returnf*x; - } the intN,M,A[MAXN],B[MAXN];Long Longans; - intMain () { -N=read (); M=read (); Inc (I,1, n) a[i]=read (), B[i]=read (); Inc (I,1, N) ans+=abs (a[i]-b[i]); -A[++n]=m; b[n]=0; Sort (A +1, a+n+1); Sort (b +1, b+n+1); Inc (I,1, N) ans+=abs (a[i]-b[i]); +printf"%lld", ans);return 0; -}
20161023
bzoj3062[usaco2013 feb]taxi*