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;
#define N 402
long long dp[N];
const long long INF = 1e18;
long long min_total_length(std::vector<int> r, std::vector<int> b) {
vector<pair<int, int >> v;
for (auto x:r) v.emplace_back(x, -1);
for (auto x:b) v.emplace_back(x, 1);
v.emplace_back(-1, -2);
sort(v.begin(), v.end());
dp[0] = 0;
for (int i = 1; i < (int) v.size(); i++) {
dp[i] = INF;
int balance = 0;
bool flag = true;
long long sum = 0;
for (int j = i; j > 0; j--) {
balance += v[j].second;
sum += v[j].first;
if (v[i].second != v[j].second && flag) {
long long cost = sum - 1ll * v[j].first * (i - j + 1);
dp[i] = min(dp[i], dp[j - 1] + cost);
// cout << i << ' ' <<sum << ' ' << v[j].first << ' ' << cost << endl;
flag = false;
}
if (balance == 0) {
long long cost = 0;
vector<int> st[3];
for (int k = j; k <= i; k++) {
st[v[k].second + 1].push_back(v[k].first);
if (!st[0].empty() && !st[2].empty()) {
cost += abs(st[2].back() - st[0].back());
st[0].pop_back();
st[1].pop_back();
}
}
dp[i] = min(dp[i], dp[j - 1] + cost);
}
}
cerr << dp[i] << ' ';
}
return dp[v.size() - 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... |