#include<bits/stdc++.h>
using namespace std;
int haha[100001][2];
long long dp[3000][3000];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,x,y;
cin >> n;
long long ans = 0;
for(int i = 1; i <= n; i++) {
haha[i][0] = 0;
haha[i][1] = 0;
}
for(int i = 0; i < 2*n; i++) {
cin >> x >> y;
if(x < 1) {
ans+=1-x;
x = 1;
}
else if(x > n) {
ans+=x-n;
x = n;
}
if(y < 1) {
ans+=1-y;
y = 1;
}
else if(y > 2) {
ans+=y-2;
y = 2;
}
haha[x][y-1]++;
}
int sb = 0;
for(int i = 0; i <= 2*n; i++) {
dp[0][i] = INT_MAX/2;
}
dp[0][0] = 0;
for(int i = 1; i <= n; i++) {
int k = haha[i][0]+haha[i][1],a = haha[i][0];
sb+=k;
for(int j = 0; j <= 2*n; j++) {
dp[i][j] = INT_MAX/2;
}
for(int j = 0; j <= 2*n; j++) {
for(int y = 0; y <= k && j+y <= 2*n; y++) {
dp[i][j+y] = min(dp[i][j+y],dp[i-1][j]+abs(i-(j+y))+abs(i-(sb-(j+y)))+abs(y-a));
}
}
}
cout << ans+dp[n][n];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
472 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
472 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
472 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
472 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
22 ms |
20056 KB |
Output is correct |
35 |
Correct |
18 ms |
20016 KB |
Output is correct |
36 |
Correct |
22 ms |
20068 KB |
Output is correct |
37 |
Correct |
23 ms |
20060 KB |
Output is correct |
38 |
Correct |
22 ms |
19944 KB |
Output is correct |
39 |
Correct |
31 ms |
20048 KB |
Output is correct |
40 |
Correct |
27 ms |
20060 KB |
Output is correct |
41 |
Correct |
17 ms |
20060 KB |
Output is correct |
42 |
Correct |
17 ms |
20060 KB |
Output is correct |
43 |
Correct |
24 ms |
20092 KB |
Output is correct |
44 |
Correct |
19 ms |
20056 KB |
Output is correct |
45 |
Correct |
20 ms |
20060 KB |
Output is correct |
46 |
Correct |
19 ms |
19980 KB |
Output is correct |
47 |
Correct |
21 ms |
20156 KB |
Output is correct |
48 |
Correct |
22 ms |
20100 KB |
Output is correct |
49 |
Correct |
21 ms |
20060 KB |
Output is correct |
50 |
Correct |
21 ms |
20204 KB |
Output is correct |
51 |
Correct |
21 ms |
20056 KB |
Output is correct |
52 |
Correct |
21 ms |
20192 KB |
Output is correct |
53 |
Correct |
22 ms |
20060 KB |
Output is correct |
54 |
Correct |
22 ms |
20084 KB |
Output is correct |
55 |
Correct |
28 ms |
20188 KB |
Output is correct |
56 |
Correct |
22 ms |
20048 KB |
Output is correct |
57 |
Correct |
21 ms |
20060 KB |
Output is correct |
58 |
Correct |
21 ms |
20060 KB |
Output is correct |
59 |
Correct |
19 ms |
20060 KB |
Output is correct |
60 |
Correct |
19 ms |
20200 KB |
Output is correct |
61 |
Correct |
18 ms |
20060 KB |
Output is correct |
62 |
Correct |
19 ms |
20048 KB |
Output is correct |
63 |
Correct |
19 ms |
20060 KB |
Output is correct |
64 |
Correct |
18 ms |
20060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
472 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
472 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
22 ms |
20056 KB |
Output is correct |
35 |
Correct |
18 ms |
20016 KB |
Output is correct |
36 |
Correct |
22 ms |
20068 KB |
Output is correct |
37 |
Correct |
23 ms |
20060 KB |
Output is correct |
38 |
Correct |
22 ms |
19944 KB |
Output is correct |
39 |
Correct |
31 ms |
20048 KB |
Output is correct |
40 |
Correct |
27 ms |
20060 KB |
Output is correct |
41 |
Correct |
17 ms |
20060 KB |
Output is correct |
42 |
Correct |
17 ms |
20060 KB |
Output is correct |
43 |
Correct |
24 ms |
20092 KB |
Output is correct |
44 |
Correct |
19 ms |
20056 KB |
Output is correct |
45 |
Correct |
20 ms |
20060 KB |
Output is correct |
46 |
Correct |
19 ms |
19980 KB |
Output is correct |
47 |
Correct |
21 ms |
20156 KB |
Output is correct |
48 |
Correct |
22 ms |
20100 KB |
Output is correct |
49 |
Correct |
21 ms |
20060 KB |
Output is correct |
50 |
Correct |
21 ms |
20204 KB |
Output is correct |
51 |
Correct |
21 ms |
20056 KB |
Output is correct |
52 |
Correct |
21 ms |
20192 KB |
Output is correct |
53 |
Correct |
22 ms |
20060 KB |
Output is correct |
54 |
Correct |
22 ms |
20084 KB |
Output is correct |
55 |
Correct |
28 ms |
20188 KB |
Output is correct |
56 |
Correct |
22 ms |
20048 KB |
Output is correct |
57 |
Correct |
21 ms |
20060 KB |
Output is correct |
58 |
Correct |
21 ms |
20060 KB |
Output is correct |
59 |
Correct |
19 ms |
20060 KB |
Output is correct |
60 |
Correct |
19 ms |
20200 KB |
Output is correct |
61 |
Correct |
18 ms |
20060 KB |
Output is correct |
62 |
Correct |
19 ms |
20048 KB |
Output is correct |
63 |
Correct |
19 ms |
20060 KB |
Output is correct |
64 |
Correct |
18 ms |
20060 KB |
Output is correct |
65 |
Execution timed out |
1020 ms |
21212 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |