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 MAX 100000000
using namespace std;
long long min_total_length(std::vector<int> r, std::vector<int> b) {
long long s=0,le,ri;
le=0,ri;
ri=b.size()-1;
if(r[r.size()-1]<b[0]){
while(true){
s+=abs(r[le]-b[ri]);
le++;
ri--;
if(le==r.size()){
if(ri==-1)
break;
else{
le--;
while(true){
s+=abs(r[le]-b[ri]);
ri--;
if(ri==-1){
break;
}
}
break;
}
}
if(ri==-1){
if(le==r.size()){
break;
}
else{
ri++;
while(true){
s+=abs(r[le]-b[ri]);
le++;
if(le==r.size()){
break;
}
}
break;
}
}
}
/*if(le!=ri){
for(int i=le;i<=ri;i++){
s+=abs(r[le-1]-b[i]);
}
}*/
return s;
}
else{
int dp[210][210];
memset(dp,MAX,sizeof dp);
dp[0][0]=abs(r[0]-b[0]);
int mi=MAX;
for(int i=0;i<r.size();i++){
for(int j=0;j<b.size();j++){
mi=MAX;
if(i==0 and j==0){
continue;
}
else{
if(i==0){
mi=min(mi,dp[i][j-1]);
}
else{
if(j==0){
mi=min(mi,dp[i-1][j]);
}
else{
mi=min(mi,dp[i-1][j-1]);
mi=min(mi,dp[i-1][j]);
mi=min(mi,dp[i][j-1]);
}
}
dp[i][j]=mi+abs(r[i]-b[j]);
}
//cout<<"i: "<<i<<" j: "<<j<<" mi: "<<mi<<" dp: "<<dp[i][j]<<endl;
}
}
/* for(int i=0;i<r.size();i++){
for(int j=0;j<b.size();j++){
cout<<dp[i][j]<<" ";
}
cout<<endl;
}*/
return dp[r.size()-1][b.size()-1];
}
}
/*
int main() {
int n, m;
assert(2 == scanf("%d %d", &n, &m));
vector<int> r(n), b(m);
for(int i = 0; i < n; i++)
assert(1 == scanf("%d", &r[i]));
for(int i = 0; i < m; i++)
assert(1 == scanf("%d", &b[i]));
long long res = min_total_length(r, b);
printf("%lld\n", res);
return 0;
}
*/
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:7:12: warning: right operand of comma operator has no effect [-Wunused-value]
7 | le=0,ri;
| ^
wiring.cpp:14:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | if(le==r.size()){
| ~~^~~~~~~~~~
wiring.cpp:30:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | if(le==r.size()){
| ~~^~~~~~~~~~
wiring.cpp:38:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | if(le==r.size()){
| ~~^~~~~~~~~~
wiring.cpp:58:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i=0;i<r.size();i++){
| ~^~~~~~~~~
wiring.cpp:59:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int j=0;j<b.size();j++){
| ~^~~~~~~~~
# | 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... |