# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1035418 | 0npata | Wiring (IOI17_wiring) | C++17 | 28 ms | 10252 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>
using namespace std;
#define vec vector
#define int long long
const int INF = 1e18;
long long min_total_length(std::vector<int32_t> r, std::vector<int32_t> b) {
vec<vec<int>> v(0);
int n = r.size();
int m = b.size();
int lst = -1;
{
int i = 0;
int j = 0;
while(i+j < n+m) {
if(i == n || (j<m && b[j] < r[i])) {
if(lst != 0) {
lst = 0;
v.push_back({});
}
v.back().push_back(b[j]);
j++;
}
else {
if(lst != 1) {
lst = 1;
v.push_back({});
}
v.back().push_back(r[i]); i++; }
}
}
vec<vec<int>> dp(v.size());
dp[0] = vec<int>(v[0].size()+1, INF);
dp[0][0] = 0;
for(int i = 1; i<v.size(); i++) {
dp[i] = vec<int>(v[i].size()+1, INF);
int k = v[i-1].size()-1;
dp[i][0] = dp[i-1].back();
int asum = 0;
int bsum = v[i-1][k];
for(int j = 1; j<=v[i].size(); j++) {
auto eval = [&]() {
int psz = v[i-1].size()-k;
return (asum - bsum) + max(j - psz, 0ll) * (-v[i-1].back()) + max(psz - j, 0ll) * v[i][0] + min(dp[i-1][k], dp[i-1][k+1]);
};
asum += v[i][j-1];
while(k > 0) {
int cur = eval();
k--;
bsum += v[i-1][k];
int nxt = eval();
if(nxt > cur && i > 1) {
bsum -= v[i-1][k];
k++;
break;
}
}
//cerr << '\n';
dp[i][j] = eval();
//cerr << k << ' ' << dp[i][j] << ' ';
}
//cerr << '\n';
}
return dp.back().back();
}
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... |