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;
ll min_total_length(vector<int> r, vector<int> b) {
int n = r.size(), m = b.size();
sort(r.begin(), r.end());
sort(b.begin(), b.end());
ll res = 0;
for (int i = 0; i < min(n, m); i ++) {
res += b[i] - r[n - 1 - i];
}
for (int i = 0; i < n - m; i ++)
res += b[0] - r[i];
for (int i = 0; i < m - n; i ++)
res += b[m - 1 - i] - r[n - 1];
return res;
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 ++) {
for (int j = 0; j <= m; 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... |