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;
typedef long long ll;
typedef unsigned long long ull;
const int N = 5e5+50;
const int oo = 2e9+7;
const int mod = 1e9+7;
vector<pair<ll,ll>> all;
ll dp[N],p[N],sum[N],B;
ll n ,m,q = 2e5;
ll get(int l, int r) {
return abs(sum[r] - sum[l - 1]);
}
ll min_total_length(vector<int> r,vector<int> b) {
n = r.size();
m = b.size();
for(int i=0;i<n;i++){
all.push_back({r[i],1});
dp[i+1] = oo;
}
for(int i=0;i<m;i++){
all.push_back({b[i],0});
dp[i+n+1] = oo;
}
r.insert(r.begin(),-oo);
r.push_back(oo);
b.insert(b.begin(),-oo);
b.push_back(oo);
n += m;
all.push_back({-oo, -1});
sort(all.begin(),all.end());
memset(p, -1, sizeof(p));
p[q] = 0;
for(int i = 1;i <= n; i++) {
sum[i] = sum[i - 1] + (all[i].second ? -all[i].first : all[i].first);
B += (all[i].second ? -1 : 1);
int j = p[B + q];
p[B + q] = i;
ll nl, nr;
if (all[i].second) {
nl = *(lower_bound(b.begin(), b.end(), all[i].first) - 1);
nr = *lower_bound(b.begin(), b.end(), all[i].first);
}
else {
nl = *(lower_bound(r.begin(),r.end(), all[i].first) - 1);
nr = *lower_bound(r.begin(), r.end(), all[i].first);
}
dp[i] = dp[i - 1] + min(all[i].first - nl, nr - all[i].first);
if(j == -1) continue;
dp[i] = min(dp[i], dp[j] + get(j + 1, 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... |