This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//grader.cpp
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define all(x) begin(x), end(x)
#define sz(x) ((int) (x).size())
using ll = long long;
using ld = long double;
const int N = 2e5 + 10;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll dp[N], pref[N], fend[N];
ll min_total_length(vector<int> r, vector<int> b) {
vector<pair<int, int>> a;
for (int u : r) {
a.push_back({u, 0});
}
for (int u : b) {
a.push_back({u, 1});
}
sort(all(a));
int k = sz(a);
fend[k - 1] = k;
for (int i = k - 1; i--; ) {
if (a[i].y != a[i + 1].y) {
fend[i] = i + 1;
} else {
fend[i] = fend[i] + 1;
}
}
for (int i = 0; i < k; ++i) {
pref[i + 1] = pref[i] + a[i].x;
}
fill(all(dp), +INF);
dp[0] = pref[fend[0]];
pair<int, int> lst = {0, 0};
for (int i = 0; i < k; ++i) {
if (i && a[i].y != a[i - 1].y) {
lst = {lst.y, i};
}
if (lst.y) {
for (int j = lst.x; j <= lst.y; ++j) {
ll balance = i + j - 2 * lst.y + 1, ans = 0;
if (balance < 0) {
ans -= a[lst.y].x * balance;
} else {
ans -= a[lst.y - 1].x * balance;
}
if (dp[i + 1] > ans + dp[j]) {
dp[i + 1] = ans + dp[j];
}
}
dp[i + 1] += pref[i + 1] - pref[lst.y];
dp[i + 1] -= pref[fend[i]] - pref[i + 1];
}
}
return dp[k];
}
# | 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... |