#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
#include <unordered_set>
#include <bitset>
#include <time.h>
#include <limits.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define szz(x) (int)x.size()
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) x.begin(),x.end()
typedef tuple<int, int, int> t3;
int N;
pii p[100010];
ll ans;
ll dp[1010][1010];
void upd(ll &a, ll b) { if(a > b) a = b; }
ll dis(pii a, pii b) { return (ll)abs(a.Fi - b.Fi) + abs(a.Se - b.Se); }
int main() {
scanf("%d", &N);
for(int i=1;i<=2*N;i++) {
int x, y; scanf("%d%d", &x, &y);
if(x < 1) ans += 1 - x, x = 1;
if(x > N) ans += x - N, x = N;
if(y < 1) ans += 1 - y, y = 1;
if(y > 2) ans += y - 2, y = 2;
p[i] = pii(x, y);
}
sort(p+1, p+1+2*N);
if(N > 1000) return 0;
for(int i=0;i<=N;i++) for(int j=0;j<=N;j++) dp[i][j] = 1e18;
dp[0][0] = 0;
for(int i=0;i<=N;i++) for(int j=0;j<=N;j++) {
pii pv = p[i + j + 1];
if(i < N) {
upd(dp[i+1][j], dis(pv, pii(i+1, 1)) + dp[i][j]);
}
if(j < N) {
upd(dp[i][j+1], dis(pv, pii(j+1, 2)) + dp[i][j]);
}
}
printf("%lld\n", ans + dp[N][N]);
return 0;
}
Compilation message
joi2019_ho_t4.cpp: In function 'int main()':
joi2019_ho_t4.cpp:41:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
joi2019_ho_t4.cpp:43:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x, y; scanf("%d%d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
3 ms |
384 KB |
Output is correct |
32 |
Correct |
2 ms |
384 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
3 ms |
384 KB |
Output is correct |
32 |
Correct |
2 ms |
384 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
20 ms |
8192 KB |
Output is correct |
35 |
Correct |
16 ms |
8320 KB |
Output is correct |
36 |
Correct |
13 ms |
8192 KB |
Output is correct |
37 |
Correct |
15 ms |
8320 KB |
Output is correct |
38 |
Correct |
20 ms |
8320 KB |
Output is correct |
39 |
Correct |
18 ms |
8320 KB |
Output is correct |
40 |
Correct |
20 ms |
8192 KB |
Output is correct |
41 |
Correct |
20 ms |
8192 KB |
Output is correct |
42 |
Correct |
16 ms |
8192 KB |
Output is correct |
43 |
Correct |
17 ms |
8320 KB |
Output is correct |
44 |
Correct |
14 ms |
8364 KB |
Output is correct |
45 |
Correct |
16 ms |
8320 KB |
Output is correct |
46 |
Correct |
16 ms |
8192 KB |
Output is correct |
47 |
Correct |
14 ms |
8320 KB |
Output is correct |
48 |
Correct |
16 ms |
8320 KB |
Output is correct |
49 |
Correct |
17 ms |
8320 KB |
Output is correct |
50 |
Correct |
14 ms |
8320 KB |
Output is correct |
51 |
Correct |
15 ms |
8320 KB |
Output is correct |
52 |
Correct |
15 ms |
8192 KB |
Output is correct |
53 |
Correct |
16 ms |
8320 KB |
Output is correct |
54 |
Correct |
15 ms |
8192 KB |
Output is correct |
55 |
Correct |
15 ms |
8320 KB |
Output is correct |
56 |
Correct |
14 ms |
8192 KB |
Output is correct |
57 |
Correct |
14 ms |
8192 KB |
Output is correct |
58 |
Correct |
15 ms |
8320 KB |
Output is correct |
59 |
Correct |
14 ms |
8192 KB |
Output is correct |
60 |
Correct |
15 ms |
8320 KB |
Output is correct |
61 |
Correct |
17 ms |
8320 KB |
Output is correct |
62 |
Correct |
19 ms |
8320 KB |
Output is correct |
63 |
Correct |
14 ms |
8320 KB |
Output is correct |
64 |
Correct |
15 ms |
8320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
3 ms |
384 KB |
Output is correct |
32 |
Correct |
2 ms |
384 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
20 ms |
8192 KB |
Output is correct |
35 |
Correct |
16 ms |
8320 KB |
Output is correct |
36 |
Correct |
13 ms |
8192 KB |
Output is correct |
37 |
Correct |
15 ms |
8320 KB |
Output is correct |
38 |
Correct |
20 ms |
8320 KB |
Output is correct |
39 |
Correct |
18 ms |
8320 KB |
Output is correct |
40 |
Correct |
20 ms |
8192 KB |
Output is correct |
41 |
Correct |
20 ms |
8192 KB |
Output is correct |
42 |
Correct |
16 ms |
8192 KB |
Output is correct |
43 |
Correct |
17 ms |
8320 KB |
Output is correct |
44 |
Correct |
14 ms |
8364 KB |
Output is correct |
45 |
Correct |
16 ms |
8320 KB |
Output is correct |
46 |
Correct |
16 ms |
8192 KB |
Output is correct |
47 |
Correct |
14 ms |
8320 KB |
Output is correct |
48 |
Correct |
16 ms |
8320 KB |
Output is correct |
49 |
Correct |
17 ms |
8320 KB |
Output is correct |
50 |
Correct |
14 ms |
8320 KB |
Output is correct |
51 |
Correct |
15 ms |
8320 KB |
Output is correct |
52 |
Correct |
15 ms |
8192 KB |
Output is correct |
53 |
Correct |
16 ms |
8320 KB |
Output is correct |
54 |
Correct |
15 ms |
8192 KB |
Output is correct |
55 |
Correct |
15 ms |
8320 KB |
Output is correct |
56 |
Correct |
14 ms |
8192 KB |
Output is correct |
57 |
Correct |
14 ms |
8192 KB |
Output is correct |
58 |
Correct |
15 ms |
8320 KB |
Output is correct |
59 |
Correct |
14 ms |
8192 KB |
Output is correct |
60 |
Correct |
15 ms |
8320 KB |
Output is correct |
61 |
Correct |
17 ms |
8320 KB |
Output is correct |
62 |
Correct |
19 ms |
8320 KB |
Output is correct |
63 |
Correct |
14 ms |
8320 KB |
Output is correct |
64 |
Correct |
15 ms |
8320 KB |
Output is correct |
65 |
Execution timed out |
23 ms |
2048 KB |
Time limit exceeded (wall clock) |
66 |
Halted |
0 ms |
0 KB |
- |