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>
using namespace std;
typedef long long ll;
const ll INF = 1e18+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ii,ll>
#define ull unsigned ll
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
#include "wiring.h"
ll n,m;
vector<int>A,B;
ll dp[205][205];
ll minA[205],minB[205];
ll solve(ll i,ll j){
if(i == n && j == m){
return 0;
}
if(dp[i][j] != -1){
return dp[i][j];
}
ll ans = INF;
if(i != n && j != m){
ans = solve(i + 1,j + 1) + abs(A[i] - B[j]);
}
if(i != n){
ans = min(ans,solve(i + 1,j) + minA[i]);
}
if(j != m){
ans = min(ans,solve(i,j + 1) + minB[j]);
}
return dp[i][j] = ans;
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
memset(dp,-1,sizeof dp);
A = r;
B = b;
n = A.size();
m = B.size();
ll pos = 0;
f(i,0,n){
while(pos < B.size() && B[pos] < A[i]){
pos++;
}
minA[i] = INF;
if(pos != B.size()){
minA[i] = abs(A[i] - B[pos]);
}
if(pos != 0){
minA[i] = min(minA[i],(ll)abs(A[i] - B[pos - 1]));
}
}
pos = 0;
f(i,0,m){
while(pos < A.size() && A[pos] < B[i]){
pos++;
}
minB[i] = INF;
if(pos != A.size()){
minB[i] = abs(B[i] - A[pos]);
}
if(pos != 0){
minB[i] = min(minB[i],(ll)abs(B[i] - A[pos - 1]));
}
}
return solve(0,0);
}
/*
int main() {
int n, m;
assert(2 == scanf("%d %d", &n, &m));
vector<int> r(n), b(m);
for(int i = 0; i < n; i++)
assert(1 == scanf("%d", &r[i]));
for(int i = 0; i < m; i++)
assert(1 == scanf("%d", &b[i]));
long long res = min_total_length(r, b);
printf("%lld\n", res);
return 0;
}
4 5
1 2 3 7
0 4 5 9 10
*/
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:58:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | while(pos < B.size() && B[pos] < A[i]){
| ~~~~^~~~~~~~~~
wiring.cpp:62:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | if(pos != B.size()){
| ~~~~^~~~~~~~~~~
wiring.cpp:71:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | while(pos < A.size() && A[pos] < B[i]){
| ~~~~^~~~~~~~~~
wiring.cpp:75:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | if(pos != A.size()){
| ~~~~^~~~~~~~~~~
# | 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... |