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;
#define int ll
typedef pair<int32_t, int32_t> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 200010
#define INF (ll)1e18
#define MOD 1000000007
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
int dp[maxn];
vi A[maxn*2];
int N,K,M;
int ss[maxn], rsm[maxn], bsm[maxn];
long long min_total_length(std::vector<int32_t> r, std::vector<int32_t> b) {
N = r.size(); M = b.size(); K = max(N, M);
vpi v;
aFOR(i,r) v.pb(pi(i,0));
aFOR(i,b) v.pb(pi(i,1));
v.pb(pi(-INF,-INF));
sort(all(v));
FOR(i,0,2*K) A[i].clear();
A[K].pb(0);
ss[0] = 0;
FOR(i,1,v.size()){
if (v[i].s == 0) ss[i] = ss[i-1] - 1;
else ss[i] = ss[i-1] + 1;
A[ss[i]+K].pb(i);
}
FOR(i,1,v.size()){
rsm[i] = rsm[i-1]; bsm[i] = bsm[i-1];
if (v[i].s == 0) rsm[i] += v[i].f;
else bsm[i] += v[i].f;
}
dp[0] = 0;
FOR(i,1,v.size()){
dp[i] = INF;
if (v[i].s == 0){
auto it = lbd(b, v[i].f);
if (it != b.end()){
dp[i] = min(dp[i], dp[i-1] + *it - v[i].f);
}
if (it != b.begin()){
dp[i] = min(dp[i], dp[i-1] + v[i].f - *(--it));
}
}else{
auto it = lbd(r, v[i].f);
if (it != r.end()){
dp[i] = min(dp[i], dp[i-1] + *it - v[i].f);
}
if (it != r.begin()) dp[i] = min(dp[i], dp[i-1] + v[i].f - *(--it));
}
auto it = lbd(A[ss[i]+K], i);
if (it != A[ss[i]+K].begin()){
int j = *(--it);
//cout << "\nPAIR " << i << ' ' << j << '\n';
if (i != j) dp[i] = min(dp[i], dp[j] + abs((rsm[i] - rsm[j]) - (bsm[i] - bsm[j])));
}
//cout << dp[i] << ' ';
}
return dp[v.size()];
}
# | 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... |