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>
#include "wiring.h"
using namespace std;
long long INF = (long long)1e10;
vector<pair<int, int>> v;
vector<long long> ossz;
long long ertek(int l, int m, int r){
long long ossz1 = ossz[m-1]-ossz[l-1], ossz2 = ossz[r]-ossz[m-1];
if(m-l < r-m+1)
ossz1 += (long long)v[m-1].first*(r-2*m+l+1);
else
ossz2 += (long long)v[m].first*(2*m-r-l-1);
return ossz2 - ossz1;
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
v.resize(r.size()+b.size()+1);
int n = 0;
for(int x : r)
v[n++] = make_pair(x, 0);
for(int x : b)
v[n++] = make_pair(x, 1);
if(r[0] < b[0])
v[n++] = make_pair(-1, 0);
else
v[n++] = make_pair(-1, 1);
sort(v.begin(), v.end());
ossz.resize(n);
ossz[0] = v[0].first;
for(int i = 1; i < n; i++)
ossz[i] = ossz[i-1] + (long long)v[i].first;
vector<long long> dp(n, INF);
dp[0] = 0ll;
for(int i = 1; i < n && v[i].second == v[0].second; i++)
dp[i] = (long long)i*INF;
int lastr = -1, lastb = -1, j = -1;
for(int i = 1; i < n; i++){
if(v[i].second == 0 && (lastr == -1 || v[i-1].second != 0)){
lastr = i;
j = lastr-1;
}else if(v[i].second == 1 && (lastb == -1 || v[i-1].second != 1)){
lastb = i;
j = lastb-1;
}
if(lastr == -1 || lastb == -1)
continue;
/*while(j > min(lastb, lastr) && ertek(j, max(lastb, lastr), i)+dp[j-1] > ertek(j-1, max(lastb, lastr), i)+dp[j-2])
j--;*/
dp[i] = INF*(long long)1000;
for(int k = max(lastb, lastr)-1; k >= min(lastb, lastr); k--)
dp[i] = min(dp[i], ertek(k, max(lastb, lastr), i)+dp[k-1]);
//dp[i] = ertek(j, max(lastb, lastr), i)+dp[j-1];
}
return dp[n-1];
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:43:33: warning: variable 'j' set but not used [-Wunused-but-set-variable]
43 | int lastr = -1, lastb = -1, j = -1;
| ^
# | 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... |