This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Ignut
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18 + 123;
const int N = 1e5 + 123;
map<int, ll> dp[N];
void Renew(int i1, int j1, int i2, int j2, ll add) {
if (!dp[i1].count(j1)) dp[i1][j1] = INF;
if (!dp[i2].count(j2)) dp[i2][j2] = INF;
dp[i1][j1] = min(dp[i1][j1], dp[i2][j2] + add);
}
ll min_total_length(vector<int> r, vector<int> b) {
int n = r.size(), m = b.size();
dp[0][0] = 0;
int d = 10;
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 - d);
int hi = min(m, pos + d);
for (int j = lo; j <= hi; j ++) {
if (i < n && j > 0)
Renew(i + 1, j, i, j, abs(r[i] - b[j - 1]));
if (j < m && i > 0)
Renew(i, j + 1, i, j, abs(r[i - 1] - b[j]));
if (i < n && j < m)
Renew(i + 1, j + 1, 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 - d);
hi = min(m, pos + d);
for (int j = lo; j <= hi; j ++) {
if (i < n && j > 0)
Renew(i + 1, j, i, j, abs(r[i] - b[j - 1]));
if (j < m && i > 0)
Renew(i, j + 1, i, j, abs(r[i - 1] - b[j]));
if (i < n && j < m)
Renew(i + 1, j + 1, 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... |