#include "bits/stdc++.h"
using namespace std;
typedef long long int ll;
ll min_total_length(vector<int> r, vector<int> b){
int n = r.size();
int m = b.size();
ll dp[n][m];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
dp[i][j] = LLONG_MAX;
}
}
dp[0][0] = abs(r[0] - b[0]);
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(i > 0){
dp[i][j] = min(dp[i][j], dp[i-1][j] + abs(r[i] - b[j]));
}
if(j > 0){
dp[i][j] = min(dp[i][j], dp[i][j-1] + abs(r[i] - b[j]));
}
if(i > 0 && j > 0){
dp[i][j] = min(dp[i][j], dp[i-1][j-1] + abs(r[i] - b[j]));
}
}
}
return dp[n-1][m-1];
}
// int main(){
// int R[] = {1, 2, 3, 7};
// int B[] = {0, 4, 5, 9, 10};
// cout << min_total_length({1, 2, 3, 7}, {0, 4, 5, 9, 10}) << '\n';
// return 0;
// }
# | 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... |