# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
200414 | HellAngel | Wiring (IOI17_wiring) | C++14 | 0 ms | 0 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 <bits/stdc++.h>
using namespace std;
typedef long long ll /// dont use define int long long
void Minimize(ll &a, const ll &b)
{
a = min(a, b);
}
ll min_total_length(vector<int> r, vector<int> b)
{
vector<vector<ll>> dp(0), prf(0);
vector<pair<ll, ll>> vt;
for(auto i: r) vt.emplace_back(i, 0);
for(auto i: b) vt.emplace_back(i, 1);
sort(vt.begin(), vt.end());
int cur = 0;
while(cur < vt.size())
{
int nxt = cur;
vector<ll> nprf = {0};
for(; nxt < vt.size() && vt[nxt].second == vt[cur].second; nxt++)
{
nprf.push_back(vt[nxt].first + nprf.back());
}
cur = nxt;
vector<ll> blank(nprf.size(), (ll)1e17);
dp.push_back(blank);
prf.push_back(nprf);
}
auto Get = [&](ll x, ll y)
{
ll ans = prf[x][y] - prf[x][y - 1];
return ans;
};
dp[0][0] = 0;
for(int i = 0; i < dp.size(); i++)
{
for(int j = 0; j + 1 < dp[i].size(); j++)
{
if(i > 0) Minimize(dp[i][j + 1], dp[i][j] + Get(i, j + 1) - Get(i - 1, prf[i - 1].size() - 1));
if(i + 1 < dp.size())
{
if(prf[i].size() - j - 1 <= prf[i + 1].size() - 1)
{
Minimize(dp[i + 1][prf[i].size() - j - 1], dp[i][j] + prf[i + 1][prf[i].size() - j - 1] - prf[i][prf[i].size() - 1] + prf[i][j]);
}
Minimize(dp[i][j + 1], dp[i][j] - Get(i, j) + Get(i + 1, 1));
}
}
if(i + 1 < dp.size()) Minimize(dp[i + 1][0], dp[i].back());
else return dp[i].back();
}
return 0;
}