#include "bits/stdc++.h"
using namespace std;
typedef long long int ll;
ll min_total_length(vector<int> r, vector<int> b){
ll dp[205][205];
int n = r.size();
int m = b.size();
ll rClose[205];
ll bClose[205];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(j == 0) rClose[i] = abs(r[i] - b[j]);
else rClose[i] = min(rClose[i], (ll) abs(r[i] - b[j]));
}
}
for(int i = 0; i < m; i++){
for(int j = 0; j < m; j++){
if(j == 0) bClose[i] = abs(b[i] - r[j]);
else bClose[i] = min(bClose[i], (ll) abs(b[i] - r[j]));
}
}
for(int i = 0; i < n+1; i++){
for(int j = 0; j < m+1; j++){
dp[i][j] = LLONG_MAX;
}
}
dp[0][0] = abs(r[0] - b[0]);
for(int i = 0; i < n+1; i++){
for(int j = 0; j < m+1; j++){
if(i){
dp[i][j] = min(dp[i][j], dp[i-1][j] + rClose[i]);
}
if(j){
dp[i][j] = min(dp[i][j], dp[i][j-1] + bClose[j]);
}
if(i && j){
dp[i][j] = min(dp[i][j], dp[i-1][j-1] + min((ll) abs(r[i] - b[j]), rClose[i] + bClose[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... |