#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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8280 KB |
Output is correct |
2 |
Correct |
4 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8284 KB |
Output is correct |
4 |
Correct |
3 ms |
8284 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
5 ms |
8284 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
3 ms |
8284 KB |
Output is correct |
11 |
Correct |
3 ms |
8220 KB |
Output is correct |
12 |
Correct |
3 ms |
8284 KB |
Output is correct |
13 |
Correct |
3 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
4 ms |
8284 KB |
Output is correct |
16 |
Correct |
3 ms |
8284 KB |
Output is correct |
17 |
Correct |
3 ms |
8284 KB |
Output is correct |
18 |
Correct |
4 ms |
8284 KB |
Output is correct |
19 |
Correct |
3 ms |
8284 KB |
Output is correct |
20 |
Correct |
3 ms |
8284 KB |
Output is correct |
21 |
Correct |
3 ms |
8284 KB |
Output is correct |
22 |
Correct |
3 ms |
8284 KB |
Output is correct |
23 |
Correct |
4 ms |
8284 KB |
Output is correct |
24 |
Correct |
3 ms |
8280 KB |
Output is correct |
25 |
Correct |
3 ms |
8284 KB |
Output is correct |
26 |
Correct |
3 ms |
8284 KB |
Output is correct |
27 |
Correct |
3 ms |
8284 KB |
Output is correct |
28 |
Correct |
3 ms |
8280 KB |
Output is correct |
29 |
Correct |
3 ms |
8284 KB |
Output is correct |
30 |
Correct |
3 ms |
8284 KB |
Output is correct |
31 |
Correct |
3 ms |
8284 KB |
Output is correct |
32 |
Correct |
3 ms |
8284 KB |
Output is correct |
33 |
Correct |
3 ms |
8192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8280 KB |
Output is correct |
2 |
Correct |
4 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8284 KB |
Output is correct |
4 |
Correct |
3 ms |
8284 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
5 ms |
8284 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
3 ms |
8284 KB |
Output is correct |
11 |
Correct |
3 ms |
8220 KB |
Output is correct |
12 |
Correct |
3 ms |
8284 KB |
Output is correct |
13 |
Correct |
3 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
4 ms |
8284 KB |
Output is correct |
16 |
Correct |
3 ms |
8284 KB |
Output is correct |
17 |
Correct |
3 ms |
8284 KB |
Output is correct |
18 |
Correct |
4 ms |
8284 KB |
Output is correct |
19 |
Correct |
3 ms |
8284 KB |
Output is correct |
20 |
Correct |
3 ms |
8284 KB |
Output is correct |
21 |
Correct |
3 ms |
8284 KB |
Output is correct |
22 |
Correct |
3 ms |
8284 KB |
Output is correct |
23 |
Correct |
4 ms |
8284 KB |
Output is correct |
24 |
Correct |
3 ms |
8280 KB |
Output is correct |
25 |
Correct |
3 ms |
8284 KB |
Output is correct |
26 |
Correct |
3 ms |
8284 KB |
Output is correct |
27 |
Correct |
3 ms |
8284 KB |
Output is correct |
28 |
Correct |
3 ms |
8280 KB |
Output is correct |
29 |
Correct |
3 ms |
8284 KB |
Output is correct |
30 |
Correct |
3 ms |
8284 KB |
Output is correct |
31 |
Correct |
3 ms |
8284 KB |
Output is correct |
32 |
Correct |
3 ms |
8284 KB |
Output is correct |
33 |
Correct |
3 ms |
8192 KB |
Output is correct |
34 |
Correct |
7 ms |
8284 KB |
Output is correct |
35 |
Correct |
7 ms |
8284 KB |
Output is correct |
36 |
Correct |
7 ms |
8532 KB |
Output is correct |
37 |
Correct |
7 ms |
8280 KB |
Output is correct |
38 |
Correct |
7 ms |
8280 KB |
Output is correct |
39 |
Correct |
7 ms |
8284 KB |
Output is correct |
40 |
Correct |
7 ms |
8200 KB |
Output is correct |
41 |
Correct |
7 ms |
8284 KB |
Output is correct |
42 |
Correct |
7 ms |
8284 KB |
Output is correct |
43 |
Correct |
8 ms |
8280 KB |
Output is correct |
44 |
Correct |
8 ms |
8280 KB |
Output is correct |
45 |
Correct |
7 ms |
8284 KB |
Output is correct |
46 |
Correct |
7 ms |
8284 KB |
Output is correct |
47 |
Correct |
7 ms |
8344 KB |
Output is correct |
48 |
Correct |
7 ms |
8280 KB |
Output is correct |
49 |
Correct |
7 ms |
8284 KB |
Output is correct |
50 |
Correct |
7 ms |
8424 KB |
Output is correct |
51 |
Correct |
9 ms |
8280 KB |
Output is correct |
52 |
Correct |
7 ms |
8284 KB |
Output is correct |
53 |
Correct |
7 ms |
8280 KB |
Output is correct |
54 |
Correct |
7 ms |
8284 KB |
Output is correct |
55 |
Correct |
7 ms |
8424 KB |
Output is correct |
56 |
Correct |
8 ms |
8280 KB |
Output is correct |
57 |
Correct |
7 ms |
8216 KB |
Output is correct |
58 |
Correct |
7 ms |
8284 KB |
Output is correct |
59 |
Correct |
7 ms |
8284 KB |
Output is correct |
60 |
Correct |
7 ms |
8428 KB |
Output is correct |
61 |
Correct |
7 ms |
8284 KB |
Output is correct |
62 |
Correct |
7 ms |
8284 KB |
Output is correct |
63 |
Correct |
7 ms |
8284 KB |
Output is correct |
64 |
Correct |
7 ms |
8420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8280 KB |
Output is correct |
2 |
Correct |
4 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8284 KB |
Output is correct |
4 |
Correct |
3 ms |
8284 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
5 ms |
8284 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
3 ms |
8284 KB |
Output is correct |
11 |
Correct |
3 ms |
8220 KB |
Output is correct |
12 |
Correct |
3 ms |
8284 KB |
Output is correct |
13 |
Correct |
3 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
4 ms |
8284 KB |
Output is correct |
16 |
Correct |
3 ms |
8284 KB |
Output is correct |
17 |
Correct |
3 ms |
8284 KB |
Output is correct |
18 |
Correct |
4 ms |
8284 KB |
Output is correct |
19 |
Correct |
3 ms |
8284 KB |
Output is correct |
20 |
Correct |
3 ms |
8284 KB |
Output is correct |
21 |
Correct |
3 ms |
8284 KB |
Output is correct |
22 |
Correct |
3 ms |
8284 KB |
Output is correct |
23 |
Correct |
4 ms |
8284 KB |
Output is correct |
24 |
Correct |
3 ms |
8280 KB |
Output is correct |
25 |
Correct |
3 ms |
8284 KB |
Output is correct |
26 |
Correct |
3 ms |
8284 KB |
Output is correct |
27 |
Correct |
3 ms |
8284 KB |
Output is correct |
28 |
Correct |
3 ms |
8280 KB |
Output is correct |
29 |
Correct |
3 ms |
8284 KB |
Output is correct |
30 |
Correct |
3 ms |
8284 KB |
Output is correct |
31 |
Correct |
3 ms |
8284 KB |
Output is correct |
32 |
Correct |
3 ms |
8284 KB |
Output is correct |
33 |
Correct |
3 ms |
8192 KB |
Output is correct |
34 |
Correct |
7 ms |
8284 KB |
Output is correct |
35 |
Correct |
7 ms |
8284 KB |
Output is correct |
36 |
Correct |
7 ms |
8532 KB |
Output is correct |
37 |
Correct |
7 ms |
8280 KB |
Output is correct |
38 |
Correct |
7 ms |
8280 KB |
Output is correct |
39 |
Correct |
7 ms |
8284 KB |
Output is correct |
40 |
Correct |
7 ms |
8200 KB |
Output is correct |
41 |
Correct |
7 ms |
8284 KB |
Output is correct |
42 |
Correct |
7 ms |
8284 KB |
Output is correct |
43 |
Correct |
8 ms |
8280 KB |
Output is correct |
44 |
Correct |
8 ms |
8280 KB |
Output is correct |
45 |
Correct |
7 ms |
8284 KB |
Output is correct |
46 |
Correct |
7 ms |
8284 KB |
Output is correct |
47 |
Correct |
7 ms |
8344 KB |
Output is correct |
48 |
Correct |
7 ms |
8280 KB |
Output is correct |
49 |
Correct |
7 ms |
8284 KB |
Output is correct |
50 |
Correct |
7 ms |
8424 KB |
Output is correct |
51 |
Correct |
9 ms |
8280 KB |
Output is correct |
52 |
Correct |
7 ms |
8284 KB |
Output is correct |
53 |
Correct |
7 ms |
8280 KB |
Output is correct |
54 |
Correct |
7 ms |
8284 KB |
Output is correct |
55 |
Correct |
7 ms |
8424 KB |
Output is correct |
56 |
Correct |
8 ms |
8280 KB |
Output is correct |
57 |
Correct |
7 ms |
8216 KB |
Output is correct |
58 |
Correct |
7 ms |
8284 KB |
Output is correct |
59 |
Correct |
7 ms |
8284 KB |
Output is correct |
60 |
Correct |
7 ms |
8428 KB |
Output is correct |
61 |
Correct |
7 ms |
8284 KB |
Output is correct |
62 |
Correct |
7 ms |
8284 KB |
Output is correct |
63 |
Correct |
7 ms |
8284 KB |
Output is correct |
64 |
Correct |
7 ms |
8420 KB |
Output is correct |
65 |
Runtime error |
357 ms |
24352 KB |
Execution killed with signal 11 |
66 |
Halted |
0 ms |
0 KB |
- |