# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
67792 | Just_Solve_The_Problem | Wiring (IOI17_wiring) | C++11 | 0 ms | 0 KiB |
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"
#include "grader.cpp"
#define pii pair < int, int >
#define fr first
#define sc second
#define ll long long
#define pb push_back
#define sz(s) (int)s.size()
using namespace std;
const int N = (int)2e5 + 7;
const ll linf = (ll)1e18 + 7;
pii a[N];
int n, m;
ll dp[N];
int last[N];
ll pref[N];
ll min_total_length(vector<int> R, vector<int> B) {
vector < ll > r, b;
n = sz(R);
m = sz(B);
for (int i = 0; i < n; i++) r.pb(R[i]);
for (int i = 0; i < m; i++) b.pb(B[i]);
int i, j;
i = j = 0;
while (i < n && j < m) {
if (r[i] < b[j]) {
a[i + j + 1] = {r[i], 1};
i++;
} else {
a[i + j + 1] = {b[j], 0};
j++;
}
}
while (i < n) a[i + j + 1] = {r[i], 1}, i++;
while (j < m) a[i + j + 1] = {b[j], 0}, j++;
for (int i = 1; i <= n + m; i++)
dp[i] = linf;
memset(last, -1, sizeof last);
int balance = 1e5;
last[balance] = 0;
j = 0;
int q = 0;
ll lastred, lastblue;
lastred = lastblue = -linf;
b.pb(linf);
r.pb(linf);
for (int i = 1; i <= n + m; i++) {
ll x = a[i].fr;
ll k = a[i].sc;
balance += (k + k - 1);
pref[i] = pref[i - 1] + ((k == 1) ? -x : x);
if (k == 1) {
while (j < m && b[j] < x) j++;
dp[i] = dp[i - 1] + min(b[j] - x, x - lastblue);
} else {
while (q < n && r[q] < x) q++;
dp[i] = dp[i - 1] + min(r[q] - x, x - lastred);
}
if (last[balance] != -1) {
dp[i] = min(dp[i], dp[last[balance]] + abs(pref[i] - pref[last[balance]]));
}
if (k == 1) lastred = x;
else lastblue = x;
last[balance] = i;
}
return dp[n + m];
}