# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
673622 | tbzard | Wiring (IOI17_wiring) | C++14 | 1 ms | 212 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;
long long min_total_length(vector<int> r, vector<int> b){
vector<pair<int, int> > a;
for(int i=0;i<r.size();i++) a.push_back(make_pair(r[i], 0));
for(int i=0;i<b.size();i++) a.push_back(make_pair(b[i], 1));
a.push_back(make_pair(-1, -1));
int n = r.size()+b.size();
sort(a.begin(), a.end());
vector<int> sz(n+1);
vector<long long> dp(n+1);
int R = -1, B = -1;
for(int i=1;i<=n;i++) dp[i] = 1e18;
for(int i=1;i<=n;i++){
if(i == 1 || a[i].second != a[i-1].second) sz[i] = 1;
else if(a[i].second == a[i-1].second) sz[i] = sz[i-1] + 1;
if(a[i].second == 0) R = i;
else B = -1;
long long sum0 = 0, sum1 = 0;
int color0 = 0, color1 = 0;
for(int j=i;j>=1;j--){
if(color1 && a[j].second == a[i].second) break;
if(a[j].second == a[i].second){
color0++;
sum0 += a[j].first;
}
else{
color1++;
sum1 += a[j].first;
}
if(color0 >= color1) dp[i] = min(dp[i], dp[j-1] + sum0 - sum1 - (color0-color1)*1ll*a[i-sz[i]].first);
else dp[i] = min(dp[i], dp[j-1] + sum0 - sum1 + (color1-color0)*1ll*a[i-sz[i]+1].first);
}
}
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... |