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 f1r(i, a, b) for (int (i) = (a); (i) < (b); ++i)
#define f0r(i, a) f1r(i, 0, a)
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define sz(x) (int) (x).size()
#define all(v) (v).begin(), (v).end()
#ifdef LOCAL
#define dbg(...) 17;
#else
#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__);
#endif
template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) { return os << "(" << p.first << ", " << p.second << ")"; }
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) { bool f = true; os << "{"; for (const auto &x : c) { if (!f) os << ", "; f = false; os << x; } return os << "}"; }
template<typename T> void debug(string s, T x) { cerr << s << " = " << x << "\n"; }
template<typename T, typename... Args> void debug(string s, T x, Args... args) { cerr << s.substr(0, s.find(',')) << " = " << x << " | "; debug(s.substr(s.find(',') + 2), args...); }
long long min_total_length(std::vector<int> r, std::vector<int> b) {
ll ans = 0;
int sz = sz(r) + sz(b);
vector<pair<ll, ll>> pts;
for (ll x : r) pts.eb(x, 0);
for (ll x : b) pts.eb(x, 1);
sort(all(pts));
vector<pair<int, int>> ivals;
int it1 = 0;
int it2 = 0;
while (it1 != sz) {
while (it2 != sz - 1 && pts[it1].s == pts[it2 + 1].s) it2++;
ivals.eb(it1, it2);
it1 = ++it2;
}
vector<ll> pre(sz);
for (int i = 0; i < sz; i++) {
if (i == 0) pre[i] = pts[i].f;
else pre[i] = pre[i-1] + pts[i].f;
}
auto sum = [&](int l, int r) -> ll {
return pre[r] - (l ? pre[l - 1] : 0);
};
const ll INF = 1e18;
vector<ll> dp(sz, INF);
// dbg(ivals);
// first thing not done
for (int ival = 1; ival < sz(ivals); ival++) {
int pl = ivals[ival - 1].f;
int pr = ivals[ival - 1].s;
int len = pr - pl + 1;
int l = ivals[ival].f;
int r = ivals[ival].s;
ll gap = pts[l].f - pts[pr].f;
vector<ll> pref(len + 1);
vector<ll> suf(len + 1);
// less than or equal to something used
for (int i = 0; i <= len; i++) {
int hi = pr;
int lo = pr - i + 1;
if (i == 0) pref[i] = INF;
else {
ll add = pts[hi].f * (hi - lo + 1) - sum(lo, hi);
ll bef = (lo ? dp[lo - 1] : 0);
pref[i] = min(pref[i - 1], add + bef);
}
}
for (int i = len; i >= 0; i--) {
int hi = pr;
int lo = pr - i + 1;
// dbg(i, pref, suf);
if (i == 0) suf[i] = suf[i + 1];
else if (i == len) {
ll add = pts[hi].f * (hi - lo + 1) - sum(lo, hi) + i * gap;
ll bef = (lo ? dp[lo - 1] : 0);
suf[i] = add + bef;
} else {
ll add = pts[hi].f * (hi - lo + 1) - sum(lo, hi) + i * gap;
ll bef = (lo ? dp[lo - 1] : 0);
suf[i] = max(suf[i + 1], add + bef);
}
}
for (int i = l; i <= r; i++) {
ll add = pts[i].f * (i - l + 1) - sum(l, i);
int use = i - l + 1;
ll add_gap = use * gap;
dp[i] = min(dp[i], pref[min(use, sz(pref) - 1)] + add + add_gap);
if (use < sz(suf)) dp[i] = min(dp[i], suf[use] + add);
}
}
// dbg(dp);
return dp[sz - 1];
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:28:5: warning: unused variable 'ans' [-Wunused-variable]
28 | ll ans = 0;
| ^~~
# | 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... |