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"
#define ll long long
#define pb push_back
#define x first
#define y second
#define all(u) u.begin(),u.end()
#define sz(u) (int)(u.size())
using namespace std;
ll min_total_length(vector<int> a, vector<int> b){
int sza=sz(a), szb=sz(b);
if(szb>sza) swap(a,b), swap(sza,szb);
int diff=sza-szb;
ll dp[sza+1][diff+1];
for(int i=0;i<=sza;i++) for(int j=0;j<=diff;j++) dp[i][j]=1e18;
dp[0][0]=0;
for(int i=1;i<=sza;i++){
for(int j=0;j<=diff && i-1-j>=0;j++){
if(dp[i-1][j]<1e17) if(i-1-j<szb) dp[i][j]=dp[i-1][j]+(ll)abs(a[i-1]-b[i-1-j]);
ll mn=2e18;
for(int k=0;k<szb;k++) mn=min(mn,(ll)abs(a[i-1]-b[k]));
if(j>0) dp[i][j]=min(dp[i][j], dp[i-1][j-1]+mn);
}
}
return dp[sza][diff];
}
/*
int main(){
cout<<min_total_length({1,2,3,7},{0,4,5,9,10});
}*/
# | 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... |