# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
416249 | prvocislo | Wiring (IOI17_wiring) | C++17 | 78 ms | 11144 KiB |
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>
typedef long long ll;
using namespace std;
const ll inf = 1e18;
long long min_total_length(std::vector<int> r, std::vector<int> b) {
int n = r.size(), m = b.size();
vector<pair<ll, int> > rb;
for (int i = 0; i < n; i++) rb.push_back({r[i], 0});
for (int i = 0; i < m; i++) rb.push_back({b[i], 1});
sort(rb.begin(), rb.end());
vector<vector<ll> > vv;
for (int i = 0; i < rb.size(); i++)
{
if (!i || rb[i-1].second != rb[i].second) vv.push_back({});
vv.back().push_back(rb[i].first);
}
vector<ll> dp(1, 0);
for (int it = 0; it < vv.size(); it++)
{
const vector<ll> &v = vv[it];
ll nx = (it == vv.size() - 1 ? 1e10 : vv[it + 1][0]), pv = (it ? vv[it - 1].back() : -1e10);
int siz = v.size();
dp.resize(siz + 1, inf);
for (int i = 1; i <= siz; i++) dp[i] = min(dp[i], dp[i - 1] + v[0] - pv); // pridame jednu extra hranu
vector<ll> nwdp(siz + 1, inf);
ll sf = 0, pf = 0;
for (ll i : v) sf += v.back() - i;
for (int i = 0; i <= siz; i++) // kolko sparujeme s predoslym usekom
{
if (i) pf += v[i - 1] - v[0], sf -= v.back() - v[i - 1];
nwdp[siz - i] = dp[i] + pf + sf + (nx - v.back()) * (siz - i); // vyskusame dlzku prefixu a sufixu
}
for (int i = siz; i > 0; i--) nwdp[i - 1] = min(nwdp[i - 1], nwdp[i]); // zahodime jednu hranu
dp = nwdp;
}
return dp[0];
}
Compilation message (stderr)
# | 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... |