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>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
string _reset = "\u001b[0m", _yellow = "\u001b[33m", _bold = "\u001b[1m";
void DBG() { cerr << "]" << _reset << endl; }
template<class H, class...T> void DBG(H h, T ...t) {
cerr << to_string(h);
if(sizeof ...(t)) cerr << ", ";
DBG(t...);
}
#ifdef CHEISSMART
#define debug(...) cerr << _yellow << _bold << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define debug(...)
#endif
const int INF = 1e9 + 7;
const ll oo = 1e18;
ll min_total_length(vi r, vi b) {
V<pi> a;
for(int i:r) a.EB(i, 0);
for(int i:b) a.EB(i, 1);
sort(ALL(a));
V<pi> seg;
int n = a.size();
for(int i = 0; i < n; ) {
int j = i;
while(i < n && a[i].S == a[j].S) i++;
seg.EB(j, i - 1);
}
V<ll> p(n);
for(int i = 0; i < n; i++) p[i] = a[i].F + (i ? p[i - 1] : 0LL);
auto ps = [&] (int l, int r) {
return p[r] - (l ? p[l - 1] : 0LL);
};
V<ll> dp(n + 1, oo);
dp[n] = 0;
assert(int(seg.size()) >= 2);
debug(seg.size());
for(int i = int(seg.size()) - 2; i >= 0; i--) {
int l1 = seg[i].F, r1 = seg[i].S, l2 = seg[i + 1].F, r2 = seg[i + 1].S;
{
// case 1: left size <= right size
V<ll> aux(r2 - l2 + 3, oo);
for(int j = r2; j >= l2; j--) {
aux[j - l2 + 1] = min(dp[j + 1], dp[j]) + ps(l2, j) - (ll) (j - l2 + 1) * a[r1].F;
aux[j - l2 + 1] = min(aux[j - l2 + 1], aux[j - l2 + 2]);
}
for(int j = 1; j <= min(r1 - l1 + 1, r2 - l2 + 1); j++)
dp[r1 - j + 1] = min(dp[r1 - j + 1], aux[j] + (ll) j * a[r1].F - ps(r1 - j + 1, r1));
}
{
// case2: left size >= right size
ll best = oo, lz = 0;
for(int j = 1; j <= r1 - l1 + 1; j++) {
lz += a[l2].F;
if(l2 + j - 1 <= r2) best = min(best, min(dp[l2 + j], dp[l2 + j - 1]) + ps(l2, l2 + j - 1) - lz);
dp[r1 - j + 1] = min(dp[r1 - j + 1], best + lz - ps(r1 - j + 1, r1));
}
}
debug(l1, r1, l2, r2);
for(int i = l1; i <= r1; i++)
debug(i, dp[i]);
}
return dp[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... |