# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
310154 | aZvezda | 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.
const int MAX_N = 1e7 + 10;
ll dp[MAX_N];
ll sufMin[MAX_N];
long long delivery(int N, int K, int L, int p[]) {
bool gotMid = false;
int lastSmaller = -1;
int n = N, k = K;
for(int i = 0; i < n; i ++) {
if(p[i] == L / 2 && L % 2 == 0) {
gotMid = true;
}
if(p[i] <= (L - 1) / 2) {
lastSmaller = i;
}
}
for(int i = 0; i < n; i ++) {
if(i >= k - 1) {
ll cost = mod * mod;
if(i - k + 1 <= lastSmaller && lastSmaller < i) {
cost = 2 * p[lastSmaller] + 2 * L - 2 * p[lastSmaller + 1];
} else if(i - k + 1 <= lastSmaller && lastSmaller <= i) {
cost = 2 * p[lastSmaller];
} else if(i <= lastSmaller) {
cost = 2 * p[i];
} else {
cost = 2 * L - 2 * p[i - k + 1];
}
dp[i] = dp[i - k + 1] + cost;
}
//cout << dp[i] << " ";
}
for(int i = n - 1; i > 0; i --) {
if(i == n - 1) {
sufMin[i] = dp[i];
} else {
sufMin[i] = min(dp[i] + 2 * L - 2 * p[i + 1], sufMin[i + 1]);
}
}
ll ret = mod * mod;
for(int i = 1; i <= k; i ++) {
chkmin(ret, p[i - 1] * 2 + sufMin[n - i]);
}
return ret;
}