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 "wiring.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Pos { ll x; int t; };
ll min_total_length(vector<int> r, vector<int> b) {
vector<Pos> a;
a.push_back({-1LL, -1});
for(int x : r) a.push_back({x, 0});
for(int x : b) a.push_back({x, 1});
sort(a.begin(), a.end(), [](Pos &u, Pos &v){ return u.x < v.x; });
int n = int(a.size()) - 1;
const static ll INF = ll(1e18);
vector<ll> d(n + 1, 0), s(n + 1, 0);
for(int i = 1; i <= n; i++) s[i] = s[i - 1] + a[i].x;
for(int i = 1; a[i].t == a[1].t; i++) d[i] = INF;
vector<ll> pm(n + 1), sm(n + 1);
for(int i = 1; i <= n - 1; i++) {
if(a[i].t == a[i + 1].t) continue;
int L, R;
for(L = i; L > 1 && a[L - 1].t == a[i].t; L--);
for(R = i + 1; R < n && a[R + 1].t == a[i + 1].t; R++);
ll md = d[i];
for(int j = i; j >= L; j--) {
md = min(md, d[j - 1]);
pm[j] = md - 2 * s[i] + s[j - 1] - (j - 2 * i - 1) * a[i + 1].x;
sm[j] = md - 2 * s[i] + s[j - 1] - (j - 2 * i - 1) * a[i].x;
}
for(int j = L + 1; j <= i; j++) pm[j] = min(pm[j], pm[j - 1]);
for(int j = i - 1; j >= L; j--) sm[j] = min(sm[j], sm[j + 1]);
for(int j = i + 1; j <= R; j++) {
int B = max(L - 1, 2 * i - j);
d[j] = min(
sm[B + 1] + s[j] - j * a[i].x,
(B < L ? INF : pm[B] + s[j] - j * a[i + 1].x)
);
}
}
return d[n];
}
# | 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... |