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>
#define fi first
#define se second
#define ssize(x) int(x.size())
#define pn printf("\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int inf = 2e09; ll infll = 2e18; int mod = 119<<23|1;
ll min_total_length(vector<int> r, vector<int> B){
int n = ssize(r), m = ssize(B);
vector<int> a(n+1), b(m+1);
for(int i = 1; i <= n; ++i) a[i] = r[i-1];
for(int i = 1; i <= m; ++i) b[i] = B[i-1];
if(n <= 200 && m >= 200){
vector<vector<ll>> dp(n+1, vector<ll>(m+1, infll));
dp[1][1] = abs(a[1]-b[1]);
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= m; ++j){
if(i != n) dp[i+1][j] = min(dp[i+1][j], dp[i][j]+abs(a[i+1]-b[j]));
if(j != m) dp[i][j+1] = min(dp[i][j+1], dp[i][j]+abs(a[i]-b[j+1]));
if(i != n && j != m) dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j]+abs(a[i+1]-b[j+1]));
}
}
return dp[n][m];
}
else{
if(n > m) swap(n, m), swap(a, b);
ll result = 0;
for(int i = 1; i <= m; ++i) result += b[i] > a.back() ? b[i]-a.back() : a[1]-b[i];
for(int i = 1; i <= n; ++i) result += b[1] > a[1] ? a.back()-a[i] : a[i]-a[1];
return result;
}
}
#ifdef LOCAL
int main(){
int T = 1;
for(++T; --T; ){
int n, m; scanf("%d%d", &n, &m);
vector<int> a(n), b(m);
for(int i = 0; i < n; ++i) scanf("%d", &a[i]);
for(int i = 0; i < m; ++i) scanf("%d", &b[i]);
printf("%lld\n", min_total_length(a, b));
}
return 0;
}
#endif
# | 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... |