# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
595472 | Apiram | Boxes with souvenirs (IOI15_boxes) | C++14 | 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"
#include<bits/stdc++.h>
using namespace std;
long long delivery(int N, int K, int L, int p[]) {
if (K == 1){
long long ans = 0;
for (int i = 0;i<N;++i){
ans +=min(p[i],L - p[i]) * 2;
}
return ans;
}
else if (K == N){
long long ans = min({L,(L - p[0]) * 2,p[N - 1] * 2});
for (int i = 0;i<N - 1;++i){
ans = min(ans,(long long)p[i] * 2 + (L - p[i + 1]) * 2);
}
return ans;
}
else{
long long ans = 0;
vector<int>have(N);
for (int i = 0;i<N;++i){
ans+=min(p[i],L - p[i]) * 2;
have[i] = K - 1;
}
for (int i = 1;i<N;++i){
if (have[i - 1] > 0 && min((p[i] - p[i - 1]) * 2,p[i] - p[i - 1] + L - p[i] - p[i - 1]) < min(p[i],L - p[i]) * 2){
ans-=min(p[i],L - p[i]) * 2;
ans+=min((p[i] - p[i - 1]) * 2,p[i] - p[i - 1] + L - p[i] - p[i - 1]);
have[i] = have[i - 1] - 1;
have[i - 1] = 0;
}
}
for (int i = N - 2;i>=0;--i){
if (have[i + 1] > 0 && have[i] == K - 1 && min((p[i + 1] - p[i]) * 2,p[i + 1] - p[i] + p[i] - (L - p[i + 1])) < min(p[i],L - p[i])*2){
ans-=min(p[i],L - p[i])*2;
ans+=min((p[i + 1] - p[i]) * 2,p[i + 1] - p[i] + p[i] - (L - p[i + 1]));
have[i] = have[i + 1] - 1;
have[i] = 0;
}
}
return ans;
//first compute the total distance we have to travel to satisfy the teams
}
return 0;
}
int main() {
int N, K, L, i;
cin>>N>>K>>L;
int *p = (int*)malloc(sizeof(int) * (unsigned int)N);
for (i = 0; i < N; i++) {
cin>>p[i];
}
cout<<delivery(N, K, L, p)<<'\n';
return 0;
}