이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// Ignut
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18 + 123;
ll min_total_length(vector<int> r, vector<int> b) {
int n = r.size(), m = b.size();
ll dp[n + 1][m + 1];
for (int i = 0; i <= n; i ++)
for (int j = 0; j <= m; j ++)
dp[i][j] = INF;
dp[0][0] = 0;
for (int i = 0; i <= n; i ++) {
int pos = lower_bound(b.begin(), b.end(), (i == 0 ? 0 : r[i - 1])) - b.begin();
int lo = max(0, pos - 3);
int hi = min(m, pos + 3);
for (int j = lo; j <= hi; j ++) {
if (i < n && j > 0)
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + abs(r[i] - b[j - 1]));
if (j < m && i > 0)
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + abs(r[i - 1] - b[j]));
if (i < n && j < m)
dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + abs(r[i] - b[j]));
}
pos = lower_bound(b.begin(), b.end(), i == m ? r[i - 1] : r[i]) - b.begin();
lo = max(0, pos - 3);
hi = min(m, pos + 3);
for (int j = lo; j <= hi; j ++) {
if (i < n && j > 0)
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + abs(r[i] - b[j - 1]));
if (j < m && i > 0)
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + abs(r[i - 1] - b[j]));
if (i < n && j < m)
dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + abs(r[i] - b[j]));
}
}
return dp[n][m];
}
# | 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... |