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 <bits/stdc++.h>
#include "wiring.h"
using namespace std;
using ll = long long;
const ll INF = 1LL<<60;
void minset(ll &p, ll v) {
if (v < p) p = v;
}
string fmt(ll x) {
if (x == INF) return "-";
return to_string(x);
}
ll min_total_length(vector<int> r, vector<int> b) {
const int n = (int)r.size(), m = (int)b.size();
if ((ll)n*m < 1e8) {
vector<vector<ll>> dp(n+1, vector<ll>(m+1, INF));
dp[0][0] = 0;
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= m; ++j) {
if (i < n && j < m) minset(dp[i+1][j+1], dp[i][j] + abs(r[i]-b[j]));
if (i && j < m) minset(dp[i][j+1], dp[i][j] + abs(r[i-1]-b[j]));
if (j && i < n) minset(dp[i+1][j], dp[i][j] + abs(r[i]-b[j-1]));
}
}
return dp[n][m];
}
// 2nd subtask: red < blue
ll ans = 0;
int s = min(n, m);
int ro = n-s;
for (int k = 0; k < s; ++k)
ans += abs(r[ro+k] - b[k]);
for (int k = 0; k < ro; ++k)
ans += abs(r[k] - b[0]);
for (int k = s; k < m; ++k)
ans += abs(r[n-1] - b[k]);
return ans;
}
# | 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... |