# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
217838 | DavidDamian | Boxes with souvenirs (IOI15_boxes) | C++11 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#include "boxes.h"
using namespace std;
typedef long long ll;
long long delivery(int N, int K, int L, int p[]) {
ll total=0;
int remaining=K;
int a=0,b=N-1;
int last=-1;
while(a<=b){
ll distA;
ll distB;
int selected=0;
if(remaining==K){
distA=min(p[a],L-p[a]);
distB=min(p[b],L-p[b]);
if(distA<=distB) selected=a;
else selected=b;
total+=(selected==a)? distA : distB;
}
else{
distA=min(abs(p[a]-p[last]),L-abs(p[a]-p[last]));
distB=min(abs(p[b]-p[last]),L-abs(p[b]-p[last]));
if(distA<=distB) selected=a;
else selected=b;
total+=(selected==a)? distA : distB;
}
remaining--;
if(remaining==0 || a==b){
distA=min(p[a],L-p[a]);
distB=min(p[b],L-p[b]);
total+=(selected==a)? distA : distB;
remaining=K;
}
last=selected;
if(selected==a) a++;
else b--;
}
return total;
}