#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();
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);
ll ans = 0;
for(ll x: C) ans += x;
for(ll x: D) ans += x;
if(n < m) swap(n, m), swap(r, b);
vector<pair<ll, int>> dp(n);
// for(ll x: r) pt.pb({x, 0});
// for(ll x: b) pt.pb({x, 1});
dp[0] = {abs(r[0] - b[0]), 0};
for(int i = 1; i < n; ++i){
dp[i] = {dp[i - 1].ff + abs(r[i] - b[dp[i - 1].ss]), dp[i - 1].ss};
if(dp[i - 1].ss + 1 < m){
ll nxt = dp[i - 1].ff + abs(r[i] - b[dp[i - 1].ss + 1]);
if(dp[i].ff >= nxt || n - i == m - dp[i - 1].ss){
dp[i] = {nxt, dp[i - 1].ss + 1};
}
}
}
return dp[n-1].ff;
}
# | 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... |