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;
#define ll long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 2e3 + 5;
const int oo = 1e9 + 7, mod = 1e9 + 7;
/*
New algo:
*/
ll n, m;
ll r[N], b[N];
ll dp[N][N];
ll pref[N][N], mini[N];
ll min_total_length(vector<int> R, vector<int> B){
n = R.size(), m = B.size();
for(int i = 0; i < n; i++) r[i + 1] = R[i];
for(int i = 0; i < m; i++) b[i + 1] = B[i];
for(int i = 0; i <= n; i++){
for(int j = 0; j <= m; j++) dp[i][j] = oo;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++) pref[i][j] = pref[i][j - 1] + abs(r[i] - b[j]);
mini[i] = oo;
for(int j = 1; j <= m; j++) mini[i] = min(mini[i], abs(r[i] - b[j]));
}
//cout << pref[1][1] << "\n";
dp[0][0] = 0;
for(int i = 0; i <= n; i++){
for(int j = 0; j <= m; j++){
for(int nxt = j + 1; nxt <= m; nxt++){
dp[i + 1][nxt] = min(dp[i + 1][nxt], dp[i][j] + pref[i + 1][nxt] - pref[i + 1][j]);
}
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + mini[i + 1]);
//cout << i << " " << j << " " << dp[i][j] << "\n";
}
}
return dp[n][m];
}
/*
void process(){
int n, m;
cin >> n >> m;
vector<int> v1, v2;
for(int i = 0; i < n; i++){
int x;
cin >> x;
v1.pb(x);
}
for(int i = 0; i < m; i++){
int x;
cin >> x;
v2.pb(x);
}
cout << min_total_length(v1, v2);
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
process();
}
*/
# | 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... |