#include<cstdio>
#include<algorithm>
#include<vector>
#define N_ 201000
using namespace std;
long long res;
struct point {
int x, y;
}w[N_];
int C[N_][2], n, S[N_], T[N_];
void Right(int b, int e) {
int i;
int s1 = 0, s2 = 0;
for (i = b; i <= e; i++) {
s1 += C[i][0];
s2 += C[i][1];
if (i == e && s1 + s2 == 1) {
C[i][0] = s1;
C[i][1] = s2;
continue;
}
if (s1 == 0) {
C[i][0] = C[i][1] = 1;
s2 -= 2;
res++;
}
else if (s2 == 0) {
C[i][0] = C[i][1] = 1;
s1 -= 2;
res++;
}
else {
C[i][0] = C[i][1] = 1;
s1--, s2--;
}
}
}
void Go(int b, int e, int pv) {
int i;
int s1 = 0, s2 = 0;
for (i = b; i < pv; i++) {
s1 += C[i][0];
s2 += C[i][1];
if (s1 > i-b+1) {
int t = s1 - (i - b + 1);
C[i][0] -= t;
C[i][1] += t;
res += t;
s1 -= t;
s2 += t;
}
if(s2 > i-b+1){
int t = s2 - (i - b + 1);
C[i][0] += t;
C[i][1] -= t;
res += t;
s1 += t;
s2 -= t;
}
}
s1 = pv - b - s1;
s2 = pv - b - s2;
if (s1 > C[pv][0]) {
int t = s1 - C[pv][0];
C[pv][0] += t;
C[pv][1] -= t;
res += t;
}
if (s2 > C[pv][1]) {
int t = s2 - C[pv][1];
C[pv][0] -= t;
C[pv][1] += t;
res += t;
}
C[pv][0] -= s1;
C[pv][1] -= s2;
for (i = b; i < pv; i++) {
C[i][0] = C[i][1] = 1;
}
Right(pv, e);
}
int main() {
int i, j;
scanf("%d", &n);
for (i = 1; i <= n * 2; i++) {
scanf("%d%d", &w[i].x, &w[i].y);
if (w[i].x < 1) {
res += 1 - w[i].x;
w[i].x = 1;
}
if (w[i].x > n) {
res += w[i].x - n;
w[i].x = n;
}
if (w[i].y < 1) {
res += 1 - w[i].y;
w[i].y = 1;
}
if (w[i].y > 2) {
res += w[i].y - 2;
w[i].y = 2;
}
C[w[i].x][w[i].y - 1]++;
}
for (i = 1; i <= n; i++) {
S[i] = S[i - 1] + C[i][0] + C[i][1];
T[i] = S[i] - i * 2;
}
for (i = 1; i < n; i++) {
res += abs(T[i]);
}
for (i = 1; i < n; i++) {
if (T[i] == 0)continue;
if (T[i] > 0) {
for (j = i; j < n; j++)if (T[j] <= 0)break;
Right(i, j);
i = j - 1;
continue;
}
else {
for (j = i; j < n; j++)if (T[j] >= 0)break;
int pv = j;
if (T[pv] > 0) {
for (j = pv; j < n; j++)if (T[j] <= 0)break;
Go(i, j, pv);
i = j - 1;
}
else {
Go(i, pv, pv);
i = pv - 1;
}
}
}
for (i = 1; i <= n; i++) {
res += abs(C[i][0] - C[i][1]) / 2;
}
printf("%lld\n", res);
}
Compilation message
joi2019_ho_t4.cpp: In function 'int main()':
joi2019_ho_t4.cpp:84: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:86:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &w[i].x, &w[i].y);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
356 KB |
Output is correct |
21 |
Correct |
2 ms |
256 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
256 KB |
Output is correct |
25 |
Correct |
2 ms |
256 KB |
Output is correct |
26 |
Correct |
2 ms |
256 KB |
Output is correct |
27 |
Correct |
2 ms |
256 KB |
Output is correct |
28 |
Correct |
2 ms |
256 KB |
Output is correct |
29 |
Correct |
2 ms |
256 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
256 KB |
Output is correct |
32 |
Correct |
2 ms |
256 KB |
Output is correct |
33 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
356 KB |
Output is correct |
21 |
Correct |
2 ms |
256 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
256 KB |
Output is correct |
25 |
Correct |
2 ms |
256 KB |
Output is correct |
26 |
Correct |
2 ms |
256 KB |
Output is correct |
27 |
Correct |
2 ms |
256 KB |
Output is correct |
28 |
Correct |
2 ms |
256 KB |
Output is correct |
29 |
Correct |
2 ms |
256 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
256 KB |
Output is correct |
32 |
Correct |
2 ms |
256 KB |
Output is correct |
33 |
Correct |
2 ms |
256 KB |
Output is correct |
34 |
Correct |
3 ms |
384 KB |
Output is correct |
35 |
Correct |
2 ms |
384 KB |
Output is correct |
36 |
Correct |
3 ms |
384 KB |
Output is correct |
37 |
Correct |
2 ms |
384 KB |
Output is correct |
38 |
Correct |
3 ms |
384 KB |
Output is correct |
39 |
Correct |
2 ms |
384 KB |
Output is correct |
40 |
Correct |
3 ms |
384 KB |
Output is correct |
41 |
Correct |
3 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
3 ms |
384 KB |
Output is correct |
44 |
Correct |
2 ms |
384 KB |
Output is correct |
45 |
Correct |
3 ms |
384 KB |
Output is correct |
46 |
Correct |
3 ms |
384 KB |
Output is correct |
47 |
Correct |
3 ms |
384 KB |
Output is correct |
48 |
Correct |
2 ms |
384 KB |
Output is correct |
49 |
Correct |
3 ms |
384 KB |
Output is correct |
50 |
Correct |
3 ms |
384 KB |
Output is correct |
51 |
Correct |
3 ms |
384 KB |
Output is correct |
52 |
Correct |
3 ms |
384 KB |
Output is correct |
53 |
Correct |
3 ms |
384 KB |
Output is correct |
54 |
Correct |
4 ms |
384 KB |
Output is correct |
55 |
Correct |
3 ms |
384 KB |
Output is correct |
56 |
Correct |
3 ms |
384 KB |
Output is correct |
57 |
Correct |
2 ms |
384 KB |
Output is correct |
58 |
Correct |
3 ms |
384 KB |
Output is correct |
59 |
Correct |
3 ms |
384 KB |
Output is correct |
60 |
Correct |
3 ms |
384 KB |
Output is correct |
61 |
Correct |
3 ms |
384 KB |
Output is correct |
62 |
Correct |
3 ms |
384 KB |
Output is correct |
63 |
Correct |
2 ms |
384 KB |
Output is correct |
64 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
356 KB |
Output is correct |
21 |
Correct |
2 ms |
256 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
256 KB |
Output is correct |
25 |
Correct |
2 ms |
256 KB |
Output is correct |
26 |
Correct |
2 ms |
256 KB |
Output is correct |
27 |
Correct |
2 ms |
256 KB |
Output is correct |
28 |
Correct |
2 ms |
256 KB |
Output is correct |
29 |
Correct |
2 ms |
256 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
256 KB |
Output is correct |
32 |
Correct |
2 ms |
256 KB |
Output is correct |
33 |
Correct |
2 ms |
256 KB |
Output is correct |
34 |
Correct |
3 ms |
384 KB |
Output is correct |
35 |
Correct |
2 ms |
384 KB |
Output is correct |
36 |
Correct |
3 ms |
384 KB |
Output is correct |
37 |
Correct |
2 ms |
384 KB |
Output is correct |
38 |
Correct |
3 ms |
384 KB |
Output is correct |
39 |
Correct |
2 ms |
384 KB |
Output is correct |
40 |
Correct |
3 ms |
384 KB |
Output is correct |
41 |
Correct |
3 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
3 ms |
384 KB |
Output is correct |
44 |
Correct |
2 ms |
384 KB |
Output is correct |
45 |
Correct |
3 ms |
384 KB |
Output is correct |
46 |
Correct |
3 ms |
384 KB |
Output is correct |
47 |
Correct |
3 ms |
384 KB |
Output is correct |
48 |
Correct |
2 ms |
384 KB |
Output is correct |
49 |
Correct |
3 ms |
384 KB |
Output is correct |
50 |
Correct |
3 ms |
384 KB |
Output is correct |
51 |
Correct |
3 ms |
384 KB |
Output is correct |
52 |
Correct |
3 ms |
384 KB |
Output is correct |
53 |
Correct |
3 ms |
384 KB |
Output is correct |
54 |
Correct |
4 ms |
384 KB |
Output is correct |
55 |
Correct |
3 ms |
384 KB |
Output is correct |
56 |
Correct |
3 ms |
384 KB |
Output is correct |
57 |
Correct |
2 ms |
384 KB |
Output is correct |
58 |
Correct |
3 ms |
384 KB |
Output is correct |
59 |
Correct |
3 ms |
384 KB |
Output is correct |
60 |
Correct |
3 ms |
384 KB |
Output is correct |
61 |
Correct |
3 ms |
384 KB |
Output is correct |
62 |
Correct |
3 ms |
384 KB |
Output is correct |
63 |
Correct |
2 ms |
384 KB |
Output is correct |
64 |
Correct |
3 ms |
384 KB |
Output is correct |
65 |
Correct |
64 ms |
4912 KB |
Output is correct |
66 |
Correct |
58 ms |
8028 KB |
Output is correct |
67 |
Correct |
81 ms |
6648 KB |
Output is correct |
68 |
Correct |
74 ms |
6632 KB |
Output is correct |
69 |
Correct |
65 ms |
6648 KB |
Output is correct |
70 |
Correct |
66 ms |
6520 KB |
Output is correct |
71 |
Correct |
65 ms |
6520 KB |
Output is correct |
72 |
Correct |
51 ms |
6648 KB |
Output is correct |
73 |
Correct |
93 ms |
6648 KB |
Output is correct |
74 |
Correct |
54 ms |
6520 KB |
Output is correct |
75 |
Correct |
59 ms |
6596 KB |
Output is correct |
76 |
Correct |
61 ms |
6648 KB |
Output is correct |
77 |
Correct |
60 ms |
6528 KB |
Output is correct |
78 |
Correct |
66 ms |
6652 KB |
Output is correct |
79 |
Correct |
62 ms |
6624 KB |
Output is correct |
80 |
Correct |
60 ms |
6652 KB |
Output is correct |
81 |
Correct |
60 ms |
6632 KB |
Output is correct |
82 |
Correct |
58 ms |
6520 KB |
Output is correct |
83 |
Correct |
61 ms |
6648 KB |
Output is correct |
84 |
Correct |
64 ms |
6648 KB |
Output is correct |
85 |
Correct |
62 ms |
6520 KB |
Output is correct |
86 |
Correct |
62 ms |
6520 KB |
Output is correct |
87 |
Correct |
53 ms |
6520 KB |
Output is correct |
88 |
Correct |
57 ms |
6648 KB |
Output is correct |
89 |
Correct |
56 ms |
6520 KB |
Output is correct |
90 |
Correct |
58 ms |
6516 KB |
Output is correct |
91 |
Correct |
58 ms |
6648 KB |
Output is correct |
92 |
Correct |
58 ms |
6648 KB |
Output is correct |
93 |
Correct |
59 ms |
6648 KB |
Output is correct |
94 |
Correct |
61 ms |
6520 KB |
Output is correct |
95 |
Correct |
94 ms |
6520 KB |
Output is correct |
96 |
Correct |
58 ms |
6648 KB |
Output is correct |
97 |
Correct |
59 ms |
6660 KB |
Output is correct |
98 |
Correct |
65 ms |
6648 KB |
Output is correct |
99 |
Correct |
74 ms |
6520 KB |
Output is correct |
100 |
Correct |
67 ms |
6520 KB |
Output is correct |
101 |
Correct |
62 ms |
6648 KB |
Output is correct |
102 |
Correct |
54 ms |
6648 KB |
Output is correct |
103 |
Correct |
54 ms |
6648 KB |
Output is correct |