# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1011132 | Roman70 | Wiring (IOI17_wiring) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// wiring.cpp
#include "wiring.h"
#include <vector>
#include <algorithm>
#include <cmath>
long long min_total_length(std::vector<int> r, std::vector<int> b) {
int n = r.size();
int m = b.size();
// DP table initialized to large values
std::vector<std::vector<long long>> dp(n + 1, std::vector<long long>(m + 1, LLONG_MAX));
// Base case: no points, no wires
dp[0][0] = 0;
// Fill the DP table
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= m; ++j) {
if (i > 0 && j > 0) {
dp[i][j] = std::min(dp[i][j], dp[i-1][j-1] + std::abs(r[i-1] - b[j-1]));
}
if (i > 0) {
dp[i][j] = std::min(dp[i][j], dp[i-1][j] + std::abs(r[i-1] - b[j-1]));
}
if (j > 0) {
dp[i][j] = std::min(dp[i][j], dp[i][j-1] + std::abs(r[i-1] - b[j-1]));
}
}
}
return dp[n][m];
}