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 pair<ll,ll> pii;
const int INF = 1e9+10;
const ll LINF = 1e18+10;
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define mk make_pair
#define pb push_back
#define f first
#define s second
ll min_total_length(vector<int> r, vector<int> b) {
vector<pii> p;
r.pb(INF);
b.pb(INF);
p.pb({-1,-1});
for(int i = 0, j = 0; r[i] != b[j]; ) {
if(r[i] < b[j]) p.pb({r[i],0}), i++;
else p.pb({b[j],1}), j++;
}
int n = sz(p);
vector<ll> dp(n, LINF), v(n, 0), m(n, 0), prefix(n, 0);
dp[0] = 0;
for(int i = 1, last = -1, at = 1; i < n; i++) {
prefix[i] = prefix[i-1] + p[i].f;
if(p[i].s != p[i-1].s) last = at, at = i;
//cout << dp[i-1] << "\n" << at << " " << p[i].f << " " << p[i].s << " ";
if(at == 1) continue;
if(i == at) {
ll d = dp[at-1];
v[at-1] = min(dp[at-1], dp[at-2]) + p[at].f - p[at-1].f;
for(int j = at-2; j >= last; j--) {
d = min(d, dp[j]);
v[j] = v[j+1] - p[j].f + p[at].f - dp[j] + min(dp[j-1],d);
}
m[last] = v[last];
for(int j = last+1; j < at; j++)
m[j] = min(m[j-1], v[j]);
dp[at] = m[at-1];
continue;
}
dp[i] = dp[i-1] + p[i].f - p[at-1].f;
if(at - (i-at+1) >= last) dp[i] = min(dp[i], m[at - (i-at+1)] - (ll)(i-at)*p[at].f + prefix[i] - prefix[at]);
}
return dp[n-1];
}
# | 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... |