// {{{1
extern "C" int __lsan_is_turned_off() { return 1; }
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define infl 0x3f3f3f3f3f3f3f3fll
#include <assert.h>
#ifdef DEBUG
#define dprintf(args...) fprintf(stderr,args)
#endif
#ifndef DEBUG
#define dprintf(args...) 69
#endif
#define all(x) (x).begin(), (x).end()
// 1}}}
void chmin(ll& x, ll y) { x=min(x,y); }
int dist(array<int,2> a, array<int,2> b) { return abs(a[0]-b[0])+abs(a[1]-b[1]); }
int main()
{
int n;scanf("%d",&n);
vector<array<int,2>> coins(2*n);
for(auto&[x,y]:coins)scanf("%d%d",&x,&y);
ll tot=0;
for(auto&[x,y]:coins) {
//printf("%d, %d -> ",x,y);
if(y < 1) tot += 1-y, y=1;
if(y > 2) tot += y-2, y=2;
if(x < 1) tot += 1-x, x=1;
if(x > n) tot += x-n, x=n;
//printf("%d, %d\n",x,y);
}
vector f(2, vector<int>(n));
for(auto&[x,y]:coins) f[y-1][x-1]++;
for(int y=0;y<2;y++)
for(int x=0;x<n;x++) f[y][x]--;
//dprintf("%lld\n",tot);
//for(int y=0;y<2;y++) {
//for(int x=0;x<n;x++) {
//dprintf("%d ",f[y][x]);
//}
//dprintf("\n");
//}
// moving a (1/-1) incurs 1 cost
for(int x=0;x<n-1;x++) {
if(f[0][x] < 0 && f[1][x] > 0) {
int del = f[0][x]+f[1][x];
if(del<=0)tot+=f[1][x],f[0][x]=del,f[1][x]=0;
else tot+=-f[0][x],f[0][x]=0,f[1][x]=del;
}
if(f[0][x] > 0 && f[1][x] < 0) {
int del = f[0][x]+f[1][x];
if(del<=0)tot+=f[0][x],f[0][x]=0,f[1][x]=del;
else tot+=-f[1][x],f[0][x]=del,f[1][x]=0;
}
//dprintf("x=%d: %lld\n",x,tot);
tot += abs(f[0][x])+abs(f[1][x]);
f[0][x+1] += f[0][x];
f[1][x+1] += f[1][x];
f[0][x]=f[1][x]=0;
//dprintf("x=%d: %lld\n",x,tot);
//for(int y=0;y<2;y++) {
//for(int x=0;x<n;x++) {
//dprintf("%d ",f[y][x]);
//}
//dprintf("\n");
//}
}
tot += abs(f[0][n-1]);
printf("%lld\n",tot);
}
Compilation message
joi2019_ho_t4.cpp: In function 'int main()':
joi2019_ho_t4.cpp:26:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | int n;scanf("%d",&n);
| ~~~~~^~~~~~~~~
joi2019_ho_t4.cpp:29:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | for(auto&[x,y]:coins)scanf("%d%d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
440 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
440 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 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 |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 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 |
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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
440 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
440 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 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 |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
480 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
480 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
1 ms |
344 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
456 KB |
Output is correct |
46 |
Correct |
1 ms |
352 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
476 KB |
Output is correct |
53 |
Correct |
1 ms |
364 KB |
Output is correct |
54 |
Correct |
1 ms |
420 KB |
Output is correct |
55 |
Correct |
1 ms |
344 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
352 KB |
Output is correct |
64 |
Correct |
1 ms |
356 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
440 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
440 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 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 |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
480 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
480 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
1 ms |
344 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
456 KB |
Output is correct |
46 |
Correct |
1 ms |
352 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
476 KB |
Output is correct |
53 |
Correct |
1 ms |
364 KB |
Output is correct |
54 |
Correct |
1 ms |
420 KB |
Output is correct |
55 |
Correct |
1 ms |
344 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
352 KB |
Output is correct |
64 |
Correct |
1 ms |
356 KB |
Output is correct |
65 |
Correct |
25 ms |
4708 KB |
Output is correct |
66 |
Correct |
35 ms |
7752 KB |
Output is correct |
67 |
Correct |
32 ms |
6236 KB |
Output is correct |
68 |
Correct |
32 ms |
6228 KB |
Output is correct |
69 |
Correct |
35 ms |
6144 KB |
Output is correct |
70 |
Correct |
34 ms |
6332 KB |
Output is correct |
71 |
Correct |
33 ms |
6200 KB |
Output is correct |
72 |
Correct |
34 ms |
6224 KB |
Output is correct |
73 |
Correct |
32 ms |
6236 KB |
Output is correct |
74 |
Correct |
32 ms |
6340 KB |
Output is correct |
75 |
Correct |
33 ms |
6224 KB |
Output is correct |
76 |
Correct |
32 ms |
6228 KB |
Output is correct |
77 |
Correct |
37 ms |
6224 KB |
Output is correct |
78 |
Correct |
32 ms |
6244 KB |
Output is correct |
79 |
Correct |
32 ms |
6224 KB |
Output is correct |
80 |
Correct |
31 ms |
6228 KB |
Output is correct |
81 |
Correct |
35 ms |
6340 KB |
Output is correct |
82 |
Correct |
32 ms |
6328 KB |
Output is correct |
83 |
Correct |
33 ms |
6212 KB |
Output is correct |
84 |
Correct |
37 ms |
6224 KB |
Output is correct |
85 |
Correct |
32 ms |
6232 KB |
Output is correct |
86 |
Correct |
32 ms |
6328 KB |
Output is correct |
87 |
Correct |
32 ms |
6344 KB |
Output is correct |
88 |
Correct |
32 ms |
6236 KB |
Output is correct |
89 |
Correct |
32 ms |
6336 KB |
Output is correct |
90 |
Correct |
32 ms |
6332 KB |
Output is correct |
91 |
Correct |
32 ms |
6224 KB |
Output is correct |
92 |
Correct |
32 ms |
6228 KB |
Output is correct |
93 |
Correct |
32 ms |
6232 KB |
Output is correct |
94 |
Correct |
30 ms |
6072 KB |
Output is correct |
95 |
Correct |
29 ms |
6212 KB |
Output is correct |
96 |
Correct |
31 ms |
6480 KB |
Output is correct |
97 |
Correct |
32 ms |
6224 KB |
Output is correct |
98 |
Correct |
32 ms |
6336 KB |
Output is correct |
99 |
Correct |
32 ms |
6216 KB |
Output is correct |
100 |
Correct |
39 ms |
6228 KB |
Output is correct |
101 |
Correct |
32 ms |
6208 KB |
Output is correct |
102 |
Correct |
54 ms |
6220 KB |
Output is correct |
103 |
Correct |
32 ms |
6224 KB |
Output is correct |