Submission #1035825

#TimeUsernameProblemLanguageResultExecution timeMemory
1035825Alihan_8Wiring (IOI17_wiring)C++17
100 / 100
54 ms10948 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...