#include "wiring.h"
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define pb push_back
#define ff first
#define ss second
#define vi vector<int>
#define ll long long int
#define all(x) x.begin(),x.end()
const int N = 250;
ll dp[N][N];
long long min_total_length(std::vector<int> R, std::vector<int> B) {
vector<ll> r(all(R));
vector<ll> b(all(B));
int n = r.size();
int m = b.size();
for(int i = 0; i <= n; ++i) for(int j = 0; j <= m; ++j) dp[i][j] = 1e18;
dp[0][0] = 0;
vector<ll> C(n);
vector<ll> D(m);
for(int i = 0; i < n; ++i){
int pos = lower_bound(all(b), r[i]) - b.begin();
if(pos == m) --pos;
ll mn = abs(b[pos] - r[i]);
if(pos>0) mn = min(mn, abs(b[pos-1] - r[i]));
C[i] = mn;
}
swap(n, m);
swap(C, D);
swap(r, b);
for(int i = 0; i < n; ++i){
int pos = lower_bound(all(b), r[i]) - b.begin();
if(pos == m) --pos;
ll mn = abs(b[pos] - r[i]);
if(pos>0) mn = min(mn, abs(b[pos-1] - r[i]));
C[i] = mn;
}
swap(n, m);
swap(C, D);
swap(r, b);
for(int i = 0; i <= n; ++i){
for(int j = 0; j <= m; ++j){
if(i+j==0) continue;
if(i==0){
dp[i][j] = dp[i][j-1] + D[j-1];
}else if(j==0){
dp[i][j] = dp[i-1][j] + C[i-1];
}else{
dp[i][j] = min({
dp[i - 1][j - 1] + abs(r[i-1]-b[j-1]),
dp[i][j - 1] + D[j-1],
dp[i - 1][j] + C[i-1]
});
}
}
}
return dp[n][m];
}
# | 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... |