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;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
//#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
const i64 inf = 1e15;
long long min_total_length(std::vector<int> r, std::vector<int> b) {
int n = r.size(), m = b.size();
vector <ar<int,2>> a;
for ( int i = 0; i < n; i++ ){
a.pb({r[i], 0});
}
for ( int i = 0; i < m; i++ ){
a.pb({b[i], 1});
}
sort(all(a));
vector <vector<int>> rng;
int lst = -1;
for ( auto &[x, t]: a ){
if ( t == lst ){
rng.back().pb(x);
} else{
rng.pb({x});
lst = t;
}
}
int k = rng.size();
auto sz = [&](int j){ return (int)rng[j].size(); };
vector <i64> dp(sz(0) + 1, inf);
dp[0] = 0;
for ( int i = 1; i < k; i++ ){
vector <i64> nxt(sz(i) + 1, inf);
auto &tmp = rng[i];
int r = dp.size(), len = tmp[0] - rng[i - 1].back();
vector <i64> f(r, inf);
i64 qt = 0;
for ( int j = r - 1; j >= 0; j-- ){
f[j] = dp[j] + qt;
if ( j > 0 ){
qt += (rng[i - 1].back() - rng[i - 1][j - 1]);
}
}
vector <i64> pf(r + 1, inf), sf(r + 1, inf);
for ( int j = r - 1; j >= 0; j-- ){
sf[j] = min(sf[j + 1], f[j]);
}
for ( int j = 0; j < r; j++ ){
if ( j > 0 ){
pf[j] = pf[j - 1];
}
chmin(pf[j], f[j] + (r - j - 1) * 1LL * len);
}
i64 cnt = 0; --r;
for ( int k = 0; k <= sz(i); k++ ){
chmin(nxt[k], cnt + k * 1LL * len + sf[max(0, r - k)]);
if ( r >= k ){
chmin(nxt[k], cnt + pf[r - k]);
}
if ( k < sz(i) ){
cnt += (tmp[k] - tmp[0]);
}
}
swap(dp, nxt);
}
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... |