#include "wiring.h"
#include <algorithm>
#include <vector>
using namespace std;
long long min_total_length(vector<int> r, vector<int> b) {
int N = r.size() + b.size();
vector<pair<int, int>> v;
for (int i = 0; i < r.size(); i++) {
v.push_back(make_pair(r[i], 0));
}
for (int i = 0; i < b.size(); i++) {
v.push_back(make_pair(b[i], 1));
}
sort(v.begin(), v.end());
vector<long long> dp(N, (long long)1e18);
for (int idx = 0; idx < N; ) {
int clb = idx, crb = idx;
while (crb < N && v[crb].second == v[clb].second) {
crb++;
}
idx = crb;
crb--;
if (clb == 0) {
continue;
}
int prb = clb - 1, plb = prb;
while (plb >= 0 && v[plb].second == v[prb].second) {
plb--;
}
plb++;
for (int c = clb; c <= crb; c++) {
for (int p = plb; p <= (plb == 0 ? plb : prb); p++) {
int i = p, j = clb;
long long cur = (plb == 0 ? 0 : dp[p - 1]);
while (i <= prb && j <= c) {
cur += v[j].first - v[i].first;
i++, j++;
}
while (i <= prb) {
cur += v[clb].first - v[i].first;
i++;
}
while (j <= c) {
cur += v[j].first - v[prb].first;
j++;
}
dp[c] = min(dp[c], cur);
}
}
}
return dp[N - 1];
}
| # | 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... |