# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1216915 | takoshanava | Boxes with souvenirs (IOI15_boxes) | C++20 | 0 ms | 0 KiB |
#include "boxes.h"
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
vector<vector<int>> cost;
vector<int> dp;
int dist1(int a, int b) {
return (b - a + L) % L;
}
int dist2(int a, int b) {
return (a - b + L) % L;
}
int delivery(int N, int K, int L, int pos[]){
vector<vector<int>> cost;
vector<int> dp;
sort(pos.begin(), pos.end(), [&](int a, int b) {
return dist1(0, a) < dist1(0, b);
});
cost.assign(N, vector<int>(N, INF));
for (int i = 0; i < N; i++) {
int far1 = 0, far2 = 0;
for (int j = i; j < N && j - i + 1 <= K; j++) {
far1 = max(far1, dist1(0, pos[j]));
cost[i][j] = min(cost[i][j], 2 * far1);
far2 = max(far2, dist2(0, pos[j]));
cost[i][j] = min(cost[i][j], 2 * far2);
if (j - i + 1 == K) {
cost[i][j] = min(cost[i][j], L);
}
}
}
dp.assign(N + 1, INF);
dp[0] = 0;
for (int i = 1; i <= N; i++) {
for (int j = max(0, i - K); j < i; j++) {
dp[i] = min(dp[i], dp[j] + cost[j][i - 1]);
}
}
return dp[N];
}