# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
43197 | szawinis | Wiring (IOI17_wiring) | C++14 | 63 ms | 9308 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>
#define pos first
#define col second
using namespace std;
using ll = long long;
const int N = 1e5+1;
const ll INF = 1e17;
int n;
vector<pair<int,int> > a;
ll s[N], dp[N];
ll min_total_length(std::vector<int> r, std::vector<int> b) {
n = r.size() + b.size();
for(int i = 0; i < r.size(); i++) a.emplace_back(r[i], 0);
for(int i = 0; i < b.size(); i++) a.emplace_back(b[i], 1);
a.emplace_back(INT_MIN, INT_MIN);
sort(a.begin(), a.end());
for(int i = 1; i <= n; i++) s[i] = s[i-1] + a[i].pos;
fill(dp+1, dp+n+1, INF);
for(int i = 2; i <= n; i++) {
// case segments completely disjoint
int k = i-1;
while(k && a[k].col == a[i].col) --k;
for(int j = k-1; a[j+1].col == a[k].col; j--) {
ll res = dp[j] + (s[i] - s[k]) - (s[k] - s[j]);
if(i-k > k-j) res -= 1ll * ((i-k) - (k-j)) * a[k].pos;
else if(i-k < k-j) res += 1ll * ((k-j) - (i-k)) * a[k+1].pos;
dp[i] = min(res, dp[i]);
}
// case segments intersect at one node
if(k-1 >= 1 && a[k-1].col == a[i].col)
dp[i] = min(dp[k] + s[i] - s[k] - 1ll * (i-k) * a[k].pos, dp[i]);
}
return dp[n];
}
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... |