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 <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
using namespace std;
template<typename T1, typename T2> inline void chkmin(T1 &a, T2 b) {if (a > b) a = b;}
template<typename T1, typename T2> inline void chkmax(T1 &a, T2 b) {if (a < b) a = b;}
#define files(FILENAME) read(FILENAME); write(FILENAME)
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define all(c) (c).begin(), (c).end()
#define sz(c) (int)(c).size()
#define left left228
#define right right228
#define y1 y1228
#define mp make_pair
#define pb push_back
#define y2 y2228
#define rank rank228
using ll = long long;
using ld = long double;
const string FILENAME = "input";
ll min_total_length(vector<int> r, vector<int> b) {
vector<int> pos[2];
pos[0] = r;
pos[1] = b;
vector<pair<int, int>> v;
for (auto x: r) {
v.pb(mp(x, 0));
}
for (auto x: b) {
v.pb(mp(x, 1));
}
sort(all(v));
vector<ll> dp(sz(v), 1e18);
map<int, int> mem;
mem[0] = -1;
int pr = 0;
vector<ll> sum[2];
sum[0].resize(sz(v));
sum[1].resize(sz(v));
for (int i = 0; i < sz(v); i++) {
if (i > 0) {
sum[0][i] = sum[0][i - 1];
sum[1][i] = sum[1][i - 1];
}
sum[v[i].second][i] += v[i].first;
if (v[i].second == 1) {
pr++;
} else {
pr--;
}
int op = v[i].second ^ 1;
int ind = lower_bound(all(pos[op]), v[i].first) - pos[op].begin();
ll close = 1e18;
for (int d : {ind, ind - 1}) {
if (0 <= d && d < sz(pos[op])) {
chkmin(close, (ll)abs(pos[op][d] - v[i].first));
}
}
dp[i] = (i == 0 ? 0 : dp[i - 1]) + close;
if (mem.count(pr)) {
int l = mem[pr] + 1;
ll offset = sum[v[i].second][i] - ((l - 1 >= 0) ? sum[v[i].second][l - 1] : 0);
offset -= sum[1 - v[i].second][i] - ((l - 1 >= 0) ? sum[1 - v[i].second][l - 1] : 0);
chkmin(dp[i], offset + ((l - 1 >= 0) ? dp[l - 1] : 0));
}
mem[pr] = i;
}
return dp.back();
}
# | 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... |