#include <stdio.h>
#define N 100000
int abs_(int a) { return a > 0 ? a : -a; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int xx[N * 4], yy[N * 4], tt[N * 4];
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (xx[ii[j]] == xx[i_])
j++;
else if (xx[ii[j]] < xx[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
long long dist(int i, int j) {
return (long long) abs_(xx[j] - xx[i]) + abs_(yy[j] - yy[i]);
}
int main() {
static int ii[N * 4], qu[2][N * 4], cnt[2];
static char used[N * 4];
int n, h, i, i_, j, d, t;
long long ans;
scanf("%d", &n);
for (i = 0; i < n * 2; i++)
scanf("%d%d", &xx[i], &yy[i]);
for (i = 0; i < n * 2; i++)
xx[n * 2 + i] = i / 2 + 1, yy[n * 2 + i] = i % 2 + 1;
for (i = 0; i < n * 4; i++)
tt[i] = yy[i] <= 1 ? 0 : 1;
for (i = 0; i < n * 4; i++)
ii[i] = i;
sort(ii, 0, n * 4);
d = 0, ans = 0;
for (i = 0; i < n * 4; i = j) {
j = i + 1;
while (j < n * 4 && xx[ii[j]] == xx[ii[i]])
j++;
if (d > 0) {
for (h = i; h < j; h++) {
i_ = ii[h], t = tt[i_];
if (i_ < n * 2)
qu[t][cnt[t]++] = i_, d++;
}
for (h = i; h < j; h++) {
i_ = ii[h], t = tt[i_];
if (i_ >= n * 2 && cnt[t])
ans += dist(i_, qu[t][--cnt[t]]), d--, used[i_] = 1;
}
for (h = i; h < j; h++) {
i_ = ii[h], t = tt[i_];
if (i_ >= n * 2 && !used[i_]) {
if (d > 0)
ans += dist(i_, qu[t ^ 1][--cnt[t ^ 1]]);
else
qu[t][cnt[t]++] = i_;
d--;
}
}
} else {
for (h = i; h < j; h++) {
i_ = ii[h], t = tt[i_];
if (i_ >= n * 2)
qu[t][cnt[t]++] = i_, d--;
}
for (h = i; h < j; h++) {
i_ = ii[h], t = tt[i_];
if (i_ < n * 2 && cnt[t])
ans += dist(i_, qu[t][--cnt[t]]), d++, used[i_] = 1;
}
for (h = i; h < j; h++) {
i_ = ii[h], t = tt[i_];
if (i_ < n * 2 && !used[i_]) {
if (d < 0)
ans += dist(i_, qu[t ^ 1][--cnt[t ^ 1]]);
else
qu[t][cnt[t]++] = i_;
d++;
}
}
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message
joi2019_ho_t4.c: In function 'main':
joi2019_ho_t4.c:44:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
joi2019_ho_t4.c:46:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | scanf("%d%d", &xx[i], &yy[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
288 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
292 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
292 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
288 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
296 KB |
Output is correct |
31 |
Correct |
1 ms |
284 KB |
Output is correct |
32 |
Correct |
0 ms |
292 KB |
Output is correct |
33 |
Correct |
0 ms |
292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
288 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
292 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
292 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
288 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
296 KB |
Output is correct |
31 |
Correct |
1 ms |
284 KB |
Output is correct |
32 |
Correct |
0 ms |
292 KB |
Output is correct |
33 |
Correct |
0 ms |
292 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
300 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
428 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
2 ms |
436 KB |
Output is correct |
44 |
Correct |
1 ms |
432 KB |
Output is correct |
45 |
Correct |
1 ms |
336 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
436 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
1 ms |
336 KB |
Output is correct |
52 |
Correct |
1 ms |
308 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
432 KB |
Output is correct |
57 |
Correct |
1 ms |
424 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
288 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
292 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
292 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
288 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
296 KB |
Output is correct |
31 |
Correct |
1 ms |
284 KB |
Output is correct |
32 |
Correct |
0 ms |
292 KB |
Output is correct |
33 |
Correct |
0 ms |
292 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
300 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
428 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
2 ms |
436 KB |
Output is correct |
44 |
Correct |
1 ms |
432 KB |
Output is correct |
45 |
Correct |
1 ms |
336 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
436 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
1 ms |
336 KB |
Output is correct |
52 |
Correct |
1 ms |
308 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
432 KB |
Output is correct |
57 |
Correct |
1 ms |
424 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
1 ms |
340 KB |
Output is correct |
65 |
Correct |
80 ms |
8196 KB |
Output is correct |
66 |
Correct |
67 ms |
12124 KB |
Output is correct |
67 |
Correct |
98 ms |
10000 KB |
Output is correct |
68 |
Correct |
96 ms |
9932 KB |
Output is correct |
69 |
Correct |
97 ms |
9992 KB |
Output is correct |
70 |
Correct |
92 ms |
9964 KB |
Output is correct |
71 |
Correct |
92 ms |
10052 KB |
Output is correct |
72 |
Correct |
58 ms |
10372 KB |
Output is correct |
73 |
Correct |
59 ms |
10660 KB |
Output is correct |
74 |
Correct |
60 ms |
10336 KB |
Output is correct |
75 |
Correct |
61 ms |
10272 KB |
Output is correct |
76 |
Correct |
61 ms |
10248 KB |
Output is correct |
77 |
Correct |
63 ms |
10176 KB |
Output is correct |
78 |
Correct |
62 ms |
10248 KB |
Output is correct |
79 |
Correct |
66 ms |
10160 KB |
Output is correct |
80 |
Correct |
63 ms |
10300 KB |
Output is correct |
81 |
Correct |
67 ms |
10320 KB |
Output is correct |
82 |
Correct |
64 ms |
10136 KB |
Output is correct |
83 |
Correct |
66 ms |
10148 KB |
Output is correct |
84 |
Correct |
74 ms |
10188 KB |
Output is correct |
85 |
Correct |
77 ms |
10108 KB |
Output is correct |
86 |
Correct |
81 ms |
10092 KB |
Output is correct |
87 |
Correct |
83 ms |
9924 KB |
Output is correct |
88 |
Correct |
90 ms |
10096 KB |
Output is correct |
89 |
Correct |
87 ms |
10024 KB |
Output is correct |
90 |
Correct |
92 ms |
9920 KB |
Output is correct |
91 |
Correct |
94 ms |
9904 KB |
Output is correct |
92 |
Correct |
92 ms |
10068 KB |
Output is correct |
93 |
Correct |
93 ms |
10016 KB |
Output is correct |
94 |
Correct |
94 ms |
9932 KB |
Output is correct |
95 |
Correct |
93 ms |
10004 KB |
Output is correct |
96 |
Correct |
74 ms |
10552 KB |
Output is correct |
97 |
Correct |
78 ms |
10432 KB |
Output is correct |
98 |
Correct |
73 ms |
10508 KB |
Output is correct |
99 |
Correct |
77 ms |
10316 KB |
Output is correct |
100 |
Correct |
78 ms |
10316 KB |
Output is correct |
101 |
Correct |
74 ms |
10316 KB |
Output is correct |
102 |
Correct |
76 ms |
10496 KB |
Output is correct |
103 |
Correct |
77 ms |
10600 KB |
Output is correct |