#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;
int n, x[200200], y[200200];
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i=1;i<=n+n;i++) cin >> x[i] >> y[i];
vector<int> A, B;
ll ans = 0;
for(int i=1;i<=n+n;i++) {
int D = min(max(x[i], 1), n);
ans += abs(D-x[i]);
if(y[i] >= 2) {
A.push_back(D);
ans += y[i]-2;
} else {
B.push_back(D);
ans += 1-y[i];
}
}
if(A.size() > B.size()) swap(A, B);
sort(A.begin(), A.end()), sort(B.begin(), B.end());
vector<vector<ll>> dp(n+1, vector<ll>(A.size()+1, INF));
dp[0][0] = 0;
for(int i=1;i<=n;i++) {
for(int a=0;a<=A.size();a++) {
int b = i+i-a;
if(b < 0 || b > B.size()) continue;
if(a >= 2) dp[i][a] = min(dp[i][a], dp[i-1][a-2] + 1 + abs(A[a-2]-i) + abs(A[a-1]-i));
if(b >= 2) dp[i][a] = min(dp[i][a], dp[i-1][a] + 1 + abs(B[b-2]-i) + abs(B[b-1]-i));
if(a >= 1 && b >= 1) dp[i][a] = min(dp[i][a], dp[i-1][a-1] + abs(B[b-1]-i) + abs(A[a-1]-i));
}
}
cout << ans + dp[n][A.size()];
}
Compilation message
joi2019_ho_t4.cpp: In function 'int main()':
joi2019_ho_t4.cpp:35:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int a=0;a<=A.size();a++) {
| ~^~~~~~~~~~
joi2019_ho_t4.cpp:37:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | if(b < 0 || b > B.size()) continue;
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
464 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
464 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
7 ms |
8280 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
7 ms |
8284 KB |
Output is correct |
37 |
Correct |
8 ms |
8280 KB |
Output is correct |
38 |
Correct |
8 ms |
8364 KB |
Output is correct |
39 |
Correct |
10 ms |
8284 KB |
Output is correct |
40 |
Correct |
7 ms |
8280 KB |
Output is correct |
41 |
Correct |
7 ms |
8244 KB |
Output is correct |
42 |
Correct |
7 ms |
8028 KB |
Output is correct |
43 |
Correct |
12 ms |
8236 KB |
Output is correct |
44 |
Correct |
7 ms |
8028 KB |
Output is correct |
45 |
Correct |
7 ms |
8536 KB |
Output is correct |
46 |
Correct |
7 ms |
8392 KB |
Output is correct |
47 |
Correct |
7 ms |
8252 KB |
Output is correct |
48 |
Correct |
7 ms |
8176 KB |
Output is correct |
49 |
Correct |
8 ms |
8284 KB |
Output is correct |
50 |
Correct |
7 ms |
8288 KB |
Output is correct |
51 |
Correct |
8 ms |
8284 KB |
Output is correct |
52 |
Correct |
7 ms |
8284 KB |
Output is correct |
53 |
Correct |
1 ms |
348 KB |
Output is correct |
54 |
Correct |
2 ms |
1884 KB |
Output is correct |
55 |
Correct |
3 ms |
1884 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
3 ms |
2088 KB |
Output is correct |
58 |
Correct |
8 ms |
6332 KB |
Output is correct |
59 |
Correct |
5 ms |
5212 KB |
Output is correct |
60 |
Correct |
6 ms |
5468 KB |
Output is correct |
61 |
Correct |
6 ms |
7004 KB |
Output is correct |
62 |
Correct |
6 ms |
6236 KB |
Output is correct |
63 |
Correct |
5 ms |
5776 KB |
Output is correct |
64 |
Correct |
4 ms |
4188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
464 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
7 ms |
8280 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
7 ms |
8284 KB |
Output is correct |
37 |
Correct |
8 ms |
8280 KB |
Output is correct |
38 |
Correct |
8 ms |
8364 KB |
Output is correct |
39 |
Correct |
10 ms |
8284 KB |
Output is correct |
40 |
Correct |
7 ms |
8280 KB |
Output is correct |
41 |
Correct |
7 ms |
8244 KB |
Output is correct |
42 |
Correct |
7 ms |
8028 KB |
Output is correct |
43 |
Correct |
12 ms |
8236 KB |
Output is correct |
44 |
Correct |
7 ms |
8028 KB |
Output is correct |
45 |
Correct |
7 ms |
8536 KB |
Output is correct |
46 |
Correct |
7 ms |
8392 KB |
Output is correct |
47 |
Correct |
7 ms |
8252 KB |
Output is correct |
48 |
Correct |
7 ms |
8176 KB |
Output is correct |
49 |
Correct |
8 ms |
8284 KB |
Output is correct |
50 |
Correct |
7 ms |
8288 KB |
Output is correct |
51 |
Correct |
8 ms |
8284 KB |
Output is correct |
52 |
Correct |
7 ms |
8284 KB |
Output is correct |
53 |
Correct |
1 ms |
348 KB |
Output is correct |
54 |
Correct |
2 ms |
1884 KB |
Output is correct |
55 |
Correct |
3 ms |
1884 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
3 ms |
2088 KB |
Output is correct |
58 |
Correct |
8 ms |
6332 KB |
Output is correct |
59 |
Correct |
5 ms |
5212 KB |
Output is correct |
60 |
Correct |
6 ms |
5468 KB |
Output is correct |
61 |
Correct |
6 ms |
7004 KB |
Output is correct |
62 |
Correct |
6 ms |
6236 KB |
Output is correct |
63 |
Correct |
5 ms |
5776 KB |
Output is correct |
64 |
Correct |
4 ms |
4188 KB |
Output is correct |
65 |
Runtime error |
154 ms |
274432 KB |
Execution killed with signal 9 |
66 |
Halted |
0 ms |
0 KB |
- |