/*
duyệt từ trái sang
đặt x là lượng coin có được
x1 là lượng coin hàng trên
dp[x1] là state có x1 coin hàng trên và x - x1 coin hàng dưới
nếu thêm 1 coin hàng trên : dp[x - 1][i] -> dp[x][i + 1]
nếu thêm 1 coin hàng dưới : dp[x - 1][i] -> dp[x][i]
*/
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define ff first
#define ss second
const ll inf = 1e18;
const int N = 2000 + 3;
int n;
pii p[N];
ll dp[N][N];
int dist(pii a, pii b) {
return abs(a.ff - b.ff) + abs(a.ss - b.ss);
}
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n * 2; ++i) cin >> p[i].ff >> p[i].ss;
sort(p + 1, p + n * 2 + 1);
ll sum(0);
for (int i = 1; i <= n * 2; ++i) {
fill(dp[i], dp[i] + i + 1, inf);
if (p[i].ff < 1) {
sum += 1 - p[i].ff;
p[i].ff = 1;
}
if (n < p[i].ff) {
sum += p[i].ff - n;
p[i].ff = n;
}
if (p[i].ss < 1) {
sum += 1 - p[i].ss;
p[i].ss = 1;
}
if (2 < p[i].ss) {
sum += p[i].ss - 2;
p[i].ss = 2;
}
for (int x = 0; x < i; ++x) {
// dp[i - 1][x] -> dp[i][x]
// go to (i - x, 2)
dp[i][x] = min(dp[i][x], dp[i - 1][x] + dist(p[i], pii(i - x, 2)));
// dp[i - 1][x] -> dp[i][x + 1]
// go to (x + 1, 1)
dp[i][x + 1] = min(dp[i][x + 1], dp[i - 1][x] + dist(p[i], pii(x + 1, 1)));
}
}
cout << sum + dp[n * 2][n] << '\n';
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
300 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
308 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
308 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
320 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
312 KB |
Output is correct |
30 |
Correct |
1 ms |
320 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
300 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
308 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
308 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
320 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
312 KB |
Output is correct |
30 |
Correct |
1 ms |
320 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
17 ms |
22908 KB |
Output is correct |
35 |
Correct |
17 ms |
23036 KB |
Output is correct |
36 |
Correct |
22 ms |
23008 KB |
Output is correct |
37 |
Correct |
22 ms |
22988 KB |
Output is correct |
38 |
Correct |
18 ms |
22968 KB |
Output is correct |
39 |
Correct |
17 ms |
23012 KB |
Output is correct |
40 |
Correct |
16 ms |
23012 KB |
Output is correct |
41 |
Correct |
20 ms |
23008 KB |
Output is correct |
42 |
Correct |
15 ms |
22996 KB |
Output is correct |
43 |
Correct |
16 ms |
22996 KB |
Output is correct |
44 |
Correct |
22 ms |
23016 KB |
Output is correct |
45 |
Correct |
16 ms |
22992 KB |
Output is correct |
46 |
Correct |
20 ms |
22968 KB |
Output is correct |
47 |
Correct |
16 ms |
23036 KB |
Output is correct |
48 |
Correct |
26 ms |
22980 KB |
Output is correct |
49 |
Correct |
22 ms |
23008 KB |
Output is correct |
50 |
Correct |
17 ms |
22972 KB |
Output is correct |
51 |
Correct |
14 ms |
23012 KB |
Output is correct |
52 |
Correct |
15 ms |
22992 KB |
Output is correct |
53 |
Correct |
16 ms |
22948 KB |
Output is correct |
54 |
Correct |
17 ms |
22988 KB |
Output is correct |
55 |
Correct |
24 ms |
22960 KB |
Output is correct |
56 |
Correct |
17 ms |
22980 KB |
Output is correct |
57 |
Correct |
17 ms |
22988 KB |
Output is correct |
58 |
Correct |
19 ms |
22988 KB |
Output is correct |
59 |
Correct |
20 ms |
22980 KB |
Output is correct |
60 |
Correct |
21 ms |
22968 KB |
Output is correct |
61 |
Correct |
21 ms |
22972 KB |
Output is correct |
62 |
Correct |
18 ms |
22976 KB |
Output is correct |
63 |
Correct |
22 ms |
22988 KB |
Output is correct |
64 |
Correct |
15 ms |
23000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
300 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
308 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
308 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
320 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
312 KB |
Output is correct |
30 |
Correct |
1 ms |
320 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
17 ms |
22908 KB |
Output is correct |
35 |
Correct |
17 ms |
23036 KB |
Output is correct |
36 |
Correct |
22 ms |
23008 KB |
Output is correct |
37 |
Correct |
22 ms |
22988 KB |
Output is correct |
38 |
Correct |
18 ms |
22968 KB |
Output is correct |
39 |
Correct |
17 ms |
23012 KB |
Output is correct |
40 |
Correct |
16 ms |
23012 KB |
Output is correct |
41 |
Correct |
20 ms |
23008 KB |
Output is correct |
42 |
Correct |
15 ms |
22996 KB |
Output is correct |
43 |
Correct |
16 ms |
22996 KB |
Output is correct |
44 |
Correct |
22 ms |
23016 KB |
Output is correct |
45 |
Correct |
16 ms |
22992 KB |
Output is correct |
46 |
Correct |
20 ms |
22968 KB |
Output is correct |
47 |
Correct |
16 ms |
23036 KB |
Output is correct |
48 |
Correct |
26 ms |
22980 KB |
Output is correct |
49 |
Correct |
22 ms |
23008 KB |
Output is correct |
50 |
Correct |
17 ms |
22972 KB |
Output is correct |
51 |
Correct |
14 ms |
23012 KB |
Output is correct |
52 |
Correct |
15 ms |
22992 KB |
Output is correct |
53 |
Correct |
16 ms |
22948 KB |
Output is correct |
54 |
Correct |
17 ms |
22988 KB |
Output is correct |
55 |
Correct |
24 ms |
22960 KB |
Output is correct |
56 |
Correct |
17 ms |
22980 KB |
Output is correct |
57 |
Correct |
17 ms |
22988 KB |
Output is correct |
58 |
Correct |
19 ms |
22988 KB |
Output is correct |
59 |
Correct |
20 ms |
22980 KB |
Output is correct |
60 |
Correct |
21 ms |
22968 KB |
Output is correct |
61 |
Correct |
21 ms |
22972 KB |
Output is correct |
62 |
Correct |
18 ms |
22976 KB |
Output is correct |
63 |
Correct |
22 ms |
22988 KB |
Output is correct |
64 |
Correct |
15 ms |
23000 KB |
Output is correct |
65 |
Runtime error |
3 ms |
572 KB |
Execution killed with signal 11 |
66 |
Halted |
0 ms |
0 KB |
- |