# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
313056 | kylych03 | 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 "wiring.h"
#include <bits/stdc++.h>
#include "grader.cpp"
using namespace std;
long long dp[200002];
long long dp1[202][202];
long long inf = 1e15;
long long fun(vector <int> x, vector <int> y){
long long ans = 0;
if(x.size() == 0 || y.size()==0)
return inf;
for(int i = 0 ; i <= x.size() ;i++){
for(int j = 0 ; j <= y.size() ; j++){
dp1[i][j]=inf;
}
}
dp1[0][0]=0;
for(int i = 0 ; i < x.size() ;i++){
for(int j = 0 ; j < y.size() ; j++){
dp1[i+1][j+1] = min(dp1[i][j] , min(dp1[i][j+1] , dp1[i+1][j] ) ) + + abs(x[i] - y[j]);
}
}
return dp1[x.size()][y.size()];
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
vector < pair <int, int> > pr;
sort(r.begin(), r.end());
sort(b.begin(), b.end());
int n = r.size();
int m = b.size();
for(int i = 0 ; i < n; i++){
pr.push_back(make_pair(r[i], 0));
}
for(int i = 0 ; i < m; i++){
pr.push_back(make_pair(b[i], 1));
}
sort(pr.begin(), pr.end());
if(r.back() < b[0]){
long long sum = 0;
for(int i = 0 ;i < r.size(); i++)
sum += (r.back() - r[i]);
for(int i = 0 ;i < b.size(); i++)
sum += (b[i] - b[0]);
return sum + max(r.size(), b.size()) * 1ll * (b[0] - r.back());
}
if(n >201 || m > 201){
for(int i = 0 ; i < n + m ; i++){
dp[i+1]=inf;
for(int j = i-1 ;j >= 0 && i-j < 7; j-- ){
vector <int> r1, b1;
for(int k = j ; k <= i ;k++){
if(pr[k].second == 0 )
r1.push_back(pr[k].first);
else
b1.push_back(pr[k].first);
}
dp[i+1] = min(dp[i+1], dp[j] + fun(r1, b1));
r1.clear();
b1.clear();
}
}
return dp[n+m];
}
return fun(r, b);
}