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;
typedef long long ll;
ll dp[1010][1010][2], dp2[15][15][15][2];
vector<int> a;
int N, K, L;
ll solve(int l, int r, int side) {
if(l == r - 1) {
if(!side) return min(a[l], L - a[l]);
else return min(a[r], L - a[r]);
}
ll &ret = dp[l][r][side];
if(ret != -1) return ret;
if(side == 0) {
ret = solve(l + 1, r, 0) + a[l + 1] - a[l];
ret = min(ret, solve(l, r - 1, 1) + a[l] + L - a[r - 1]);
} else {
ret = solve(l, r - 1, 1) + a[r] - a[r - 1];
ret = min(ret, solve(l + 1, r, 0) + a[l + 1] + L - a[r]);
}
return ret;
}
ll solve2(int l, int r, int rem, int side) {
if(l == r - 1) {
if(!side) return min(a[l], L - a[l]);
else return min(a[r], L - a[r]);
}
ll &ret = dp2[l][r][rem][side];
if(ret != -1) return ret;
if(side == 0) {
if(rem) {
ret = solve2(l + 1, r, rem - 1, 0) + a[l + 1] - a[l];
ret = min(ret, solve2(l, r - 1, rem - 1, 1) + a[l] + L - a[r - 1]);
} else {
ret = solve2(l + 1, r, K - 1, 0) + a[l + 1];
ret = min(ret, solve2(l, r - 1, K - 1, 1) + L - a[r - 1]);
ret += min(a[l], L - a[l]);
}
} else {
if(rem) {
ret = solve2(l, r - 1, rem - 1, 1) + a[r] - a[r - 1];
ret = min(ret, solve2(l + 1, r, rem - 1, 0) + a[l + 1] + L - a[r]);
} else {
ret = solve2(l, r - 1, K - 1, 1) + L - a[r - 1];
ret = min(ret, solve2(l + 1, r, K - 1, 0) + a[l + 1]);
ret += min(a[r], L - a[r]);
}
}
return ret;
}
ll delivery(int n, int k, int l, int p[]) {
N = n, K = k, L = l;
if(N <= 15) {
memset(dp2, -1, sizeof dp2);
a.push_back(0);
for(int i = 0; i < N; i++)
a.push_back(p[i]);
a.push_back(L);
return solve2(0, N + 1, K, 0);
}
if(K == 1 || N == 1) {
ll tot = 0;
for(int i = 0; i < N; i++)
tot += 2 * min(p[i], L - p[i]);
return tot;
}
if(N == K) {
memset(dp, -1, sizeof dp);
a.push_back(0);
for(int i = 0; i < N; i++)
a.push_back(p[i]);
a.push_back(L);
return solve(0, N + 1, 0);
}
}
Compilation message (stderr)
boxes.cpp: In function 'll delivery(int, int, int, int*)':
boxes.cpp:81:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |