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); //scanf("%d%d", &n, &m);
vector<int> a(n+1), b(m+1);
vector<vector<ll>> dp(n+1, vector<ll>(m+1, infll));
for(int i = 1; i <= n; ++i) a[i] = r[i-1];//scanf("%d", &a[i]);
for(int i = 1; i <= m; ++i) b[i] = B[i-1]; //scanf("%d", &b[i]);
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]));
}
}
//printf("%lld\n", dp[n][m]);
return dp[n][m];
}
#ifdef LOCAL
int main(){
int T = 1;
for(++T; --T; ) //answer();
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... |