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>
using namespace std;
int d(int x, int y) {return abs(x - y);}
long long min_total_length(vector<int> r, vector<int> b) {
int n = r.size(), m = b.size();
if (r.back() < b.front()) {
long long a = 0;
for(int i: r) a += r.back() - i;
for(int i: b) a += i - b.front();
return a + (b.front() - r.back()) * max(r.size(), b.size());
}
if (n <= 200 && m <= 200) {
int dp[201][201];
dp[0][0] = d(r[0], b[0]);
for(int i = 1; i < n; i++) dp[i][0] = dp[i-1][0] + d(r[i], b[0]);
for(int i = 1; i < m; i++) dp[0][i] = dp[0][i-1] + d(r[0], b[i]);
for(int i = 1; i < n; i++)
for(int j = 1; j < m; j++)
dp[i][j] = min({dp[i-1][j], dp[i][j-1], dp[i-1][j-1]}) + d(r[i], b[j]);
return dp[n - 1][m - 1];
}
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:26:1: warning: control reaches end of non-void function [-Wreturn-type]
26 | }
| ^
# | 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... |