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;
const int inf = 1234567890;
int n;
pair<int, int> a[1000100];
set<int> sa, sb;
int getnear(int x) {
int res = 2e9;
if (a[x].second == 1) {
auto p = sa.lower_bound(a[x].first);
if (p != sa.end()) {
res = min(res, *p - a[x].first);
}
if (p != sa.begin()) {
p--;
res = min(res, a[x].first - *p);
}
}
else {
auto p = sb.lower_bound(a[x].first);
if (p != sb.end()) {
res = min(res, *p - a[x].first);
}
if (p != sb.begin()) {
p--;
res = min(res, a[x].first - *p);
}
}
return res;
}
long long suma[1000100], sumb[1000100];
long long dp[1000100];
int mat[1000100], prv[2000200];
long long min_total_length(vector<int> aa, vector<int> bb) {
int p = (int) aa.size(), q = (int) bb.size();
for (int i = 1; i <= p; i++) {
a[i].first = aa[i - 1];
sa.insert(aa[i - 1]);
a[i].second = -1;
}
for (int i = 1; i <= q; i++) {
a[i + p].first = bb[i - 1];
sb.insert(bb[i - 1]);
a[i + p].second = 1;
}
n = p + q;
sort(a + 1, a + 1 + n);
memset(mat, 255, sizeof(mat));
memset(prv, 255, sizeof(prv));
prv[n] = 0;
int cur = n;
for (int i = 1; i <= n; i++) {
suma[i] = suma[i - 1]; sumb[i] = sumb[i - 1];
if (a[i].second == -1) {
suma[i] += a[i].first;
cur++;
}
else {
sumb[i] += a[i].first;
cur--;
}
if (prv[cur] >= 0) {
mat[i] = prv[cur];
}
prv[cur] = i;
}
for (int i = 1; i <= n; i++) {
dp[i] = dp[i - 1] + getnear(i);
if (~mat[i]) {
dp[i] = min(dp[i], dp[mat[i]] + abs(suma[i] - suma[mat[i]] - sumb[i] + sumb[mat[i]]));
}
}
return dp[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... |