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;
using ll = long long;
using vll = vector <ll>;
using ii = pair <ll, ll>;
using vii = vector <ii>;
ll dp[1005][1005];
// dp[a][b]
int main () {
cin.tie(nullptr) -> sync_with_stdio(false);
ll n;
cin >> n;
vii ve(2*n);
for (auto &[x, y] : ve) cin >> x >> y;
// ll ans = 0;
// auto fclamp = [&](ll l, ll r, ll &val) {
// if (val < l) { ans += l-val; val = l; }
// if (val > r) { ans += val-r; val = r; }
// };
// for (auto &[x, y] : ve) {
// fclamp(1, n, x);
// fclamp(1, 2, y);
// }
sort(ve.begin(), ve.end());
auto fcost = [&](ll X, ll Y, ll i) {
auto [x, y] = ve[i];
return abs(x-X) + abs(y-Y);
};
// auto shsw = [&](ll i, ll j) {
// return fcost(i, asg[j]) + fcost(j, asg[i]) < fcost(i, asg[i]) + fcost(j, asg[j]);
// };
memset(dp, 0x3f, sizeof(dp));
dp[0][0] = 0; // no coins moved, none to be assigned
for (ll c1 = 0; c1 <= n; c1++) {
for (ll c2 = 0; c2 <= n; c2++) {
ll i = c1+c2-1;
if (c1 > 0) dp[c1][c2] = min(dp[c1][c2], dp[c1-1][c2] + fcost(c1, 1, i));
if (c2 > 0) dp[c1][c2] = min(dp[c1][c2], dp[c1][c2-1] + fcost(c2, 2, i));
}
}
cout << dp[n][n] << '\n';
// for (ll k = 0; k < 2*n; k++) {
// for (ll i = 0; i < 2*n; i++) {
// for (ll j = 0; j < 2*n; j++) {
// if (shsw(i, j)) swap(asg[i], asg[j]);
// }
// }
// }
// for (ll i = 0; i < 2*n; i++) ans += cost(i, asg[i]);
// cout << ans << '\n';
return 0;
}
/*
0 0 4 0 1 2
2 0 3 0 0 0
0 0 a 0 b c
d 0 e 0 0 0
d a a b c c
d a a e e e
a a a a b c
d d e e e c
dp[i][a][b] = min cost to reach a state where the prefix
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |