#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define x first
#define y second
using namespace std;
using ll = long long;
#define int ll
void umin(int &a, int b) {
a = min(a, b);
}
void umax(int &a, int b) {
a = max(a, b);
}
const int N = 2020;
const int INF = 1e18;
int n;
int x[N];
int y[N];
int p[N];
int w[N];
int dp[N][N];
int f(int i, int fx, int fy) {
return abs(x[i] - fx) + abs(y[i] - fy);
}
signed main() {
#ifdef LC
assert(freopen("input.txt", "r", stdin));
#endif
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 0; i < 2 * n; ++i) {
cin >> x[i] >> y[i];
p[i] = i;
}
sort(p, p + 2 * n, [&](int i, int j) {
return x[i] < x[j];
});
for (int i = 0; i < 2 * n; ++i) {
w[p[i]] = i;
}
for (int i = 0; i < 2 * n; ++i) {
while (w[i] != i) {
int j = w[i];
swap(w[i], w[j]);
swap(x[i], x[j]);
swap(y[i], y[j]);
}
}
assert(is_sorted(x, x + 2 * n));
fill_n(dp[0], N * N, INF);
dp[0][0] = 0;
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
if (i < n) {
umin(dp[i + 1][j], dp[i][j] + f(i + j, i + 1, 1));
}
if (j < n) {
umin(dp[i][j + 1], dp[i][j] + f(i + j, j + 1, 2));
}
}
}
cout << dp[n][n] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
32256 KB |
Output is correct |
2 |
Correct |
15 ms |
32256 KB |
Output is correct |
3 |
Correct |
19 ms |
32248 KB |
Output is correct |
4 |
Correct |
14 ms |
32256 KB |
Output is correct |
5 |
Correct |
14 ms |
32256 KB |
Output is correct |
6 |
Correct |
14 ms |
32256 KB |
Output is correct |
7 |
Correct |
14 ms |
32256 KB |
Output is correct |
8 |
Correct |
14 ms |
32256 KB |
Output is correct |
9 |
Correct |
14 ms |
32256 KB |
Output is correct |
10 |
Correct |
14 ms |
32256 KB |
Output is correct |
11 |
Correct |
14 ms |
32256 KB |
Output is correct |
12 |
Correct |
14 ms |
32256 KB |
Output is correct |
13 |
Correct |
14 ms |
32256 KB |
Output is correct |
14 |
Correct |
17 ms |
32256 KB |
Output is correct |
15 |
Correct |
16 ms |
32248 KB |
Output is correct |
16 |
Correct |
18 ms |
32256 KB |
Output is correct |
17 |
Correct |
16 ms |
32256 KB |
Output is correct |
18 |
Correct |
17 ms |
32256 KB |
Output is correct |
19 |
Correct |
23 ms |
32248 KB |
Output is correct |
20 |
Correct |
15 ms |
32256 KB |
Output is correct |
21 |
Correct |
15 ms |
32256 KB |
Output is correct |
22 |
Correct |
19 ms |
32256 KB |
Output is correct |
23 |
Correct |
17 ms |
32384 KB |
Output is correct |
24 |
Correct |
17 ms |
32256 KB |
Output is correct |
25 |
Correct |
16 ms |
32384 KB |
Output is correct |
26 |
Correct |
19 ms |
32256 KB |
Output is correct |
27 |
Correct |
16 ms |
32256 KB |
Output is correct |
28 |
Correct |
18 ms |
32256 KB |
Output is correct |
29 |
Correct |
15 ms |
32244 KB |
Output is correct |
30 |
Correct |
18 ms |
32256 KB |
Output is correct |
31 |
Correct |
18 ms |
32256 KB |
Output is correct |
32 |
Correct |
16 ms |
32256 KB |
Output is correct |
33 |
Correct |
15 ms |
32256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
32256 KB |
Output is correct |
2 |
Correct |
15 ms |
32256 KB |
Output is correct |
3 |
Correct |
19 ms |
32248 KB |
Output is correct |
4 |
Correct |
14 ms |
32256 KB |
Output is correct |
5 |
Correct |
14 ms |
32256 KB |
Output is correct |
6 |
Correct |
14 ms |
32256 KB |
Output is correct |
7 |
Correct |
14 ms |
32256 KB |
Output is correct |
8 |
Correct |
14 ms |
32256 KB |
Output is correct |
9 |
Correct |
14 ms |
32256 KB |
Output is correct |
10 |
Correct |
14 ms |
32256 KB |
Output is correct |
11 |
Correct |
14 ms |
32256 KB |
Output is correct |
12 |
Correct |
14 ms |
32256 KB |
Output is correct |
13 |
Correct |
14 ms |
32256 KB |
Output is correct |
14 |
Correct |
17 ms |
32256 KB |
Output is correct |
15 |
Correct |
16 ms |
32248 KB |
Output is correct |
16 |
Correct |
18 ms |
32256 KB |
Output is correct |
17 |
Correct |
16 ms |
32256 KB |
Output is correct |
18 |
Correct |
17 ms |
32256 KB |
Output is correct |
19 |
Correct |
23 ms |
32248 KB |
Output is correct |
20 |
Correct |
15 ms |
32256 KB |
Output is correct |
21 |
Correct |
15 ms |
32256 KB |
Output is correct |
22 |
Correct |
19 ms |
32256 KB |
Output is correct |
23 |
Correct |
17 ms |
32384 KB |
Output is correct |
24 |
Correct |
17 ms |
32256 KB |
Output is correct |
25 |
Correct |
16 ms |
32384 KB |
Output is correct |
26 |
Correct |
19 ms |
32256 KB |
Output is correct |
27 |
Correct |
16 ms |
32256 KB |
Output is correct |
28 |
Correct |
18 ms |
32256 KB |
Output is correct |
29 |
Correct |
15 ms |
32244 KB |
Output is correct |
30 |
Correct |
18 ms |
32256 KB |
Output is correct |
31 |
Correct |
18 ms |
32256 KB |
Output is correct |
32 |
Correct |
16 ms |
32256 KB |
Output is correct |
33 |
Correct |
15 ms |
32256 KB |
Output is correct |
34 |
Correct |
34 ms |
32384 KB |
Output is correct |
35 |
Correct |
23 ms |
32384 KB |
Output is correct |
36 |
Correct |
23 ms |
32384 KB |
Output is correct |
37 |
Correct |
21 ms |
32384 KB |
Output is correct |
38 |
Correct |
21 ms |
32384 KB |
Output is correct |
39 |
Correct |
23 ms |
32384 KB |
Output is correct |
40 |
Correct |
22 ms |
32384 KB |
Output is correct |
41 |
Correct |
24 ms |
32376 KB |
Output is correct |
42 |
Correct |
21 ms |
32384 KB |
Output is correct |
43 |
Correct |
21 ms |
32384 KB |
Output is correct |
44 |
Correct |
21 ms |
32384 KB |
Output is correct |
45 |
Correct |
25 ms |
32376 KB |
Output is correct |
46 |
Correct |
24 ms |
32384 KB |
Output is correct |
47 |
Correct |
21 ms |
32384 KB |
Output is correct |
48 |
Correct |
23 ms |
32384 KB |
Output is correct |
49 |
Correct |
23 ms |
32384 KB |
Output is correct |
50 |
Correct |
35 ms |
32376 KB |
Output is correct |
51 |
Correct |
24 ms |
32376 KB |
Output is correct |
52 |
Correct |
21 ms |
32384 KB |
Output is correct |
53 |
Correct |
25 ms |
32384 KB |
Output is correct |
54 |
Correct |
24 ms |
32384 KB |
Output is correct |
55 |
Correct |
22 ms |
32384 KB |
Output is correct |
56 |
Correct |
25 ms |
32384 KB |
Output is correct |
57 |
Correct |
22 ms |
32384 KB |
Output is correct |
58 |
Correct |
21 ms |
32384 KB |
Output is correct |
59 |
Correct |
21 ms |
32384 KB |
Output is correct |
60 |
Correct |
21 ms |
32384 KB |
Output is correct |
61 |
Correct |
22 ms |
32384 KB |
Output is correct |
62 |
Correct |
21 ms |
32384 KB |
Output is correct |
63 |
Correct |
22 ms |
32384 KB |
Output is correct |
64 |
Correct |
21 ms |
32388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
32256 KB |
Output is correct |
2 |
Correct |
15 ms |
32256 KB |
Output is correct |
3 |
Correct |
19 ms |
32248 KB |
Output is correct |
4 |
Correct |
14 ms |
32256 KB |
Output is correct |
5 |
Correct |
14 ms |
32256 KB |
Output is correct |
6 |
Correct |
14 ms |
32256 KB |
Output is correct |
7 |
Correct |
14 ms |
32256 KB |
Output is correct |
8 |
Correct |
14 ms |
32256 KB |
Output is correct |
9 |
Correct |
14 ms |
32256 KB |
Output is correct |
10 |
Correct |
14 ms |
32256 KB |
Output is correct |
11 |
Correct |
14 ms |
32256 KB |
Output is correct |
12 |
Correct |
14 ms |
32256 KB |
Output is correct |
13 |
Correct |
14 ms |
32256 KB |
Output is correct |
14 |
Correct |
17 ms |
32256 KB |
Output is correct |
15 |
Correct |
16 ms |
32248 KB |
Output is correct |
16 |
Correct |
18 ms |
32256 KB |
Output is correct |
17 |
Correct |
16 ms |
32256 KB |
Output is correct |
18 |
Correct |
17 ms |
32256 KB |
Output is correct |
19 |
Correct |
23 ms |
32248 KB |
Output is correct |
20 |
Correct |
15 ms |
32256 KB |
Output is correct |
21 |
Correct |
15 ms |
32256 KB |
Output is correct |
22 |
Correct |
19 ms |
32256 KB |
Output is correct |
23 |
Correct |
17 ms |
32384 KB |
Output is correct |
24 |
Correct |
17 ms |
32256 KB |
Output is correct |
25 |
Correct |
16 ms |
32384 KB |
Output is correct |
26 |
Correct |
19 ms |
32256 KB |
Output is correct |
27 |
Correct |
16 ms |
32256 KB |
Output is correct |
28 |
Correct |
18 ms |
32256 KB |
Output is correct |
29 |
Correct |
15 ms |
32244 KB |
Output is correct |
30 |
Correct |
18 ms |
32256 KB |
Output is correct |
31 |
Correct |
18 ms |
32256 KB |
Output is correct |
32 |
Correct |
16 ms |
32256 KB |
Output is correct |
33 |
Correct |
15 ms |
32256 KB |
Output is correct |
34 |
Correct |
34 ms |
32384 KB |
Output is correct |
35 |
Correct |
23 ms |
32384 KB |
Output is correct |
36 |
Correct |
23 ms |
32384 KB |
Output is correct |
37 |
Correct |
21 ms |
32384 KB |
Output is correct |
38 |
Correct |
21 ms |
32384 KB |
Output is correct |
39 |
Correct |
23 ms |
32384 KB |
Output is correct |
40 |
Correct |
22 ms |
32384 KB |
Output is correct |
41 |
Correct |
24 ms |
32376 KB |
Output is correct |
42 |
Correct |
21 ms |
32384 KB |
Output is correct |
43 |
Correct |
21 ms |
32384 KB |
Output is correct |
44 |
Correct |
21 ms |
32384 KB |
Output is correct |
45 |
Correct |
25 ms |
32376 KB |
Output is correct |
46 |
Correct |
24 ms |
32384 KB |
Output is correct |
47 |
Correct |
21 ms |
32384 KB |
Output is correct |
48 |
Correct |
23 ms |
32384 KB |
Output is correct |
49 |
Correct |
23 ms |
32384 KB |
Output is correct |
50 |
Correct |
35 ms |
32376 KB |
Output is correct |
51 |
Correct |
24 ms |
32376 KB |
Output is correct |
52 |
Correct |
21 ms |
32384 KB |
Output is correct |
53 |
Correct |
25 ms |
32384 KB |
Output is correct |
54 |
Correct |
24 ms |
32384 KB |
Output is correct |
55 |
Correct |
22 ms |
32384 KB |
Output is correct |
56 |
Correct |
25 ms |
32384 KB |
Output is correct |
57 |
Correct |
22 ms |
32384 KB |
Output is correct |
58 |
Correct |
21 ms |
32384 KB |
Output is correct |
59 |
Correct |
21 ms |
32384 KB |
Output is correct |
60 |
Correct |
21 ms |
32384 KB |
Output is correct |
61 |
Correct |
22 ms |
32384 KB |
Output is correct |
62 |
Correct |
21 ms |
32384 KB |
Output is correct |
63 |
Correct |
22 ms |
32384 KB |
Output is correct |
64 |
Correct |
21 ms |
32388 KB |
Output is correct |
65 |
Runtime error |
3 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
66 |
Halted |
0 ms |
0 KB |
- |