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 < 14; 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);
}
Compilation message (stderr)
wiring.cpp: In function 'long long int fun(std::vector<int>, std::vector<int>)':
wiring.cpp:12:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | for(int i = 0 ; i <= x.size() ;i++){
| ~~^~~~~~~~~~~
wiring.cpp:13:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for(int j = 0 ; j <= y.size() ; j++){
| ~~^~~~~~~~~~~
wiring.cpp:18:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for(int i = 0 ; i < x.size() ;i++){
| ~~^~~~~~~~~~
wiring.cpp:19:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for(int j = 0 ; j < y.size() ; j++){
| ~~^~~~~~~~~~
wiring.cpp:9:15: warning: unused variable 'ans' [-Wunused-variable]
9 | long long ans = 0;
| ^~~
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:42:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int i = 0 ;i < r.size(); i++)
| ~~^~~~~~~~~~
wiring.cpp:44:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i = 0 ;i < b.size(); i++)
| ~~^~~~~~~~~~
# | 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... |