#include <stdio.h>
#include <string.h>
#define N 300
#define N_ (1 << 9) /* N_ = pow2(ceil(log2(N))) */
#define N2 (N * 2 + 5)
#define INF 0x7fffffff
#define LINF 0x3f3f3f3f3f3f3f3fLL
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
long long min_(long long a, long long b) { return a < b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int xx[N], yy[N], ii[N], ii_[N], n, x_, y_; long long xx_[N2];
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;
}
}
int lll[N][N], rrr[N][N], ccc[N][N];
long long solve(long long w) {
static int ii_[N2], ll[N2], rr[N2], cc[N2], qul[N2], qur[N2], quc[N2];
static long long xx1[N2];
int m, h, h_, i, il, ir, j, k, l, r, c, headl, cntl, headr, cntr, headc, cntc, ans;
if (w < 0)
return LINF;
for (i = 0; i < n; i++)
xx_[i << 1 | 0] = xx[i], xx_[i << 1 | 1] = xx[i] + w + 1;
i = 0, j = 0, k = 0;
while (i < n || j < n)
ii_[k++] = j == n || i < n && xx[ii[i]] < xx[ii[j]] + w + 1 ? ii[i++] << 1 | 0 : ii[j++] << 1 | 1;
m = 0;
ll[m] = INF, rr[m] = -1, cc[m] = INF, xx1[m] = -LINF, m++;
for (h = 0, il = ir = 0; h < n * 2; h++) {
i = ii_[h];
if ((i & 1) == 0)
ir++;
else
il++;
if (h + 1 == n * 2 || xx_[ii_[h + 1]] != xx_[ii_[h]]) {
if (il == ir)
ll[m] = INF, rr[m] = -1, cc[m] = INF;
else
ll[m] = lll[il][ir - 1], rr[m] = rrr[il][ir - 1], cc[m] = ccc[il][ir - 1];
xx1[m++] = xx_[ii_[h]];
}
}
xx1[m] = LINF;
ans = INF;
l = -1, r = INF, c = 0;
for (h = 0; h < m; h++) {
if (xx1[h + 1] <= 0 || x_ <= xx1[h])
continue;
l = max(l, ll[h]), r = min(r, rr[h]), c = max(c, cc[h]);
}
if (l != INF && r != -1 && c != INF)
ans = min(ans, max(l + y_ - 1 - r, c));
l = -1, r = INF, c = 0;
for (h = 0; h < m; h++) {
if (xx1[h + 1] <= w || w + x_ <= xx1[h])
continue;
l = max(l, ll[h]), r = min(r, rr[h]), c = max(c, cc[h]);
}
if (l != INF && r != -1 && c != INF)
ans = min(ans, max(l + y_ - 1 - r, c));
headl = cntl = 0, headr = cntr = 0, headc = cntc = 0;
for (h = 0, h_ = 0; h < m; h++) {
while (h_ < m && xx1[h_] < xx1[h] + x_) {
while (cntl && ll[qul[headl + cntl - 1]] <= ll[h_])
cntl--;
qul[headl + cntl++] = h_;
while (cntr && rr[qur[headr + cntr - 1]] >= rr[h_])
cntr--;
qur[headr + cntr++] = h_;
while (cntc && cc[quc[headc + cntc - 1]] <= cc[h_])
cntc--;
quc[headc + cntc++] = h_;
h_++;
}
if (xx1[h] >= 0 && xx1[h] <= w) {
l = ll[qul[headl]], r = rr[qur[headr]], c = cc[quc[headc]];
if (l != INF && r != -1 && c != INF)
ans = min(ans, max(l + y_ - 1 - r, c));
}
if (qul[headl] == h)
headl++, cntl--;
if (qur[headr] == h)
headr++, cntr--;
if (quc[headc] == h)
headc++, cntc--;
}
return ans == INF ? LINF : ans + w;
}
int main() {
int k, h, i, j, tmp;
long long ans;
scanf("%d%d%d", &x_, &y_, &n);
for (i = 0; i < n; i++)
scanf("%d%d", &xx[i], &yy[i]), xx[i]--, yy[i]--;
for (i = 0; i < n; i++)
ii[i] = i;
sort(ii, 0, n);
for (i = 0; i < n; i++) {
k = 0;
for (j = i; j < n; j++) {
ii_[k++] = ii[j];
for (h = k - 1; h > 0 && yy[ii_[h]] < yy[ii_[h - 1]]; h--)
tmp = ii_[h], ii_[h] = ii_[h - 1], ii_[h - 1] = tmp;
lll[i][j] = yy[ii_[0]];
rrr[i][j] = yy[ii_[k - 1]];
for (h = 1; h < k; h++)
ccc[i][j] = max(ccc[i][j], yy[ii_[h]] - yy[ii_[h - 1]] - 1);
}
}
ans = LINF;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
ans = min_(ans, min_(solve((x_ - 1 - xx[i]) + (xx[j] - 0)), solve(xx[j] - xx[i] - 1)));
printf("%lld\n", ans);
return 0;
}
Compilation message
cultivation.c: In function 'solve':
cultivation.c:54:30: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
54 | ii_[k++] = j == n || i < n && xx[ii[i]] < xx[ii[j]] + w + 1 ? ii[i++] << 1 | 0 : ii[j++] << 1 | 1;
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cultivation.c: In function 'main':
cultivation.c:122:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | scanf("%d%d%d", &x_, &y_, &n);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cultivation.c:124:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
124 | scanf("%d%d", &xx[i], &yy[i]), 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 |
340 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 |
340 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 |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 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 |
340 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 |
340 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 |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
11 ms |
548 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
7 ms |
496 KB |
Output is correct |
22 |
Correct |
63 ms |
728 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
210 ms |
1000 KB |
Output is correct |
25 |
Correct |
99 ms |
724 KB |
Output is correct |
26 |
Correct |
608 ms |
1344 KB |
Output is correct |
27 |
Correct |
585 ms |
1376 KB |
Output is correct |
28 |
Correct |
186 ms |
980 KB |
Output is correct |
29 |
Correct |
541 ms |
1344 KB |
Output is correct |
30 |
Correct |
558 ms |
1316 KB |
Output is correct |
31 |
Correct |
560 ms |
1348 KB |
Output is correct |
32 |
Correct |
550 ms |
1356 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 |
340 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 |
340 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 |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
11 ms |
548 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
7 ms |
496 KB |
Output is correct |
22 |
Correct |
63 ms |
728 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
210 ms |
1000 KB |
Output is correct |
25 |
Correct |
99 ms |
724 KB |
Output is correct |
26 |
Correct |
608 ms |
1344 KB |
Output is correct |
27 |
Correct |
585 ms |
1376 KB |
Output is correct |
28 |
Correct |
186 ms |
980 KB |
Output is correct |
29 |
Correct |
541 ms |
1344 KB |
Output is correct |
30 |
Correct |
558 ms |
1316 KB |
Output is correct |
31 |
Correct |
560 ms |
1348 KB |
Output is correct |
32 |
Correct |
550 ms |
1356 KB |
Output is correct |
33 |
Correct |
235 ms |
1340 KB |
Output is correct |
34 |
Correct |
542 ms |
1364 KB |
Output is correct |
35 |
Correct |
585 ms |
1344 KB |
Output is correct |
36 |
Correct |
595 ms |
1236 KB |
Output is correct |
37 |
Correct |
533 ms |
1344 KB |
Output is correct |
38 |
Correct |
563 ms |
1340 KB |
Output is correct |
39 |
Correct |
561 ms |
1364 KB |
Output is correct |
40 |
Correct |
566 ms |
1348 KB |
Output is correct |
41 |
Correct |
343 ms |
1344 KB |
Output is correct |
42 |
Correct |
432 ms |
1340 KB |
Output is correct |
43 |
Correct |
526 ms |
1236 KB |
Output is correct |
44 |
Correct |
573 ms |
1344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
432 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
404 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
432 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
404 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
77 ms |
652 KB |
Output is correct |
22 |
Correct |
87 ms |
596 KB |
Output is correct |
23 |
Correct |
88 ms |
648 KB |
Output is correct |
24 |
Correct |
80 ms |
652 KB |
Output is correct |
25 |
Correct |
102 ms |
652 KB |
Output is correct |
26 |
Correct |
60 ms |
596 KB |
Output is correct |
27 |
Correct |
91 ms |
652 KB |
Output is correct |
28 |
Correct |
88 ms |
652 KB |
Output is correct |
29 |
Correct |
83 ms |
652 KB |
Output is correct |
30 |
Correct |
90 ms |
652 KB |
Output is correct |
31 |
Correct |
88 ms |
648 KB |
Output is correct |
32 |
Correct |
88 ms |
596 KB |
Output is correct |
33 |
Correct |
81 ms |
656 KB |
Output is correct |
34 |
Correct |
86 ms |
596 KB |
Output is correct |
35 |
Correct |
75 ms |
648 KB |
Output is correct |
36 |
Correct |
78 ms |
596 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 |
340 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 |
340 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 |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
11 ms |
548 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
7 ms |
496 KB |
Output is correct |
22 |
Correct |
63 ms |
728 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
210 ms |
1000 KB |
Output is correct |
25 |
Correct |
99 ms |
724 KB |
Output is correct |
26 |
Correct |
608 ms |
1344 KB |
Output is correct |
27 |
Correct |
585 ms |
1376 KB |
Output is correct |
28 |
Correct |
186 ms |
980 KB |
Output is correct |
29 |
Correct |
541 ms |
1344 KB |
Output is correct |
30 |
Correct |
558 ms |
1316 KB |
Output is correct |
31 |
Correct |
560 ms |
1348 KB |
Output is correct |
32 |
Correct |
550 ms |
1356 KB |
Output is correct |
33 |
Correct |
235 ms |
1340 KB |
Output is correct |
34 |
Correct |
542 ms |
1364 KB |
Output is correct |
35 |
Correct |
585 ms |
1344 KB |
Output is correct |
36 |
Correct |
595 ms |
1236 KB |
Output is correct |
37 |
Correct |
533 ms |
1344 KB |
Output is correct |
38 |
Correct |
563 ms |
1340 KB |
Output is correct |
39 |
Correct |
561 ms |
1364 KB |
Output is correct |
40 |
Correct |
566 ms |
1348 KB |
Output is correct |
41 |
Correct |
343 ms |
1344 KB |
Output is correct |
42 |
Correct |
432 ms |
1340 KB |
Output is correct |
43 |
Correct |
526 ms |
1236 KB |
Output is correct |
44 |
Correct |
573 ms |
1344 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
2 ms |
340 KB |
Output is correct |
47 |
Correct |
2 ms |
340 KB |
Output is correct |
48 |
Correct |
2 ms |
340 KB |
Output is correct |
49 |
Correct |
2 ms |
340 KB |
Output is correct |
50 |
Correct |
2 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
2 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
3 ms |
432 KB |
Output is correct |
55 |
Correct |
2 ms |
340 KB |
Output is correct |
56 |
Correct |
2 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
2 ms |
340 KB |
Output is correct |
61 |
Correct |
2 ms |
340 KB |
Output is correct |
62 |
Correct |
2 ms |
404 KB |
Output is correct |
63 |
Correct |
2 ms |
340 KB |
Output is correct |
64 |
Correct |
2 ms |
340 KB |
Output is correct |
65 |
Correct |
77 ms |
652 KB |
Output is correct |
66 |
Correct |
87 ms |
596 KB |
Output is correct |
67 |
Correct |
88 ms |
648 KB |
Output is correct |
68 |
Correct |
80 ms |
652 KB |
Output is correct |
69 |
Correct |
102 ms |
652 KB |
Output is correct |
70 |
Correct |
60 ms |
596 KB |
Output is correct |
71 |
Correct |
91 ms |
652 KB |
Output is correct |
72 |
Correct |
88 ms |
652 KB |
Output is correct |
73 |
Correct |
83 ms |
652 KB |
Output is correct |
74 |
Correct |
90 ms |
652 KB |
Output is correct |
75 |
Correct |
88 ms |
648 KB |
Output is correct |
76 |
Correct |
88 ms |
596 KB |
Output is correct |
77 |
Correct |
81 ms |
656 KB |
Output is correct |
78 |
Correct |
86 ms |
596 KB |
Output is correct |
79 |
Correct |
75 ms |
648 KB |
Output is correct |
80 |
Correct |
78 ms |
596 KB |
Output is correct |
81 |
Correct |
923 ms |
1072 KB |
Output is correct |
82 |
Correct |
901 ms |
1084 KB |
Output is correct |
83 |
Correct |
1267 ms |
1228 KB |
Output is correct |
84 |
Correct |
1325 ms |
1228 KB |
Output is correct |
85 |
Execution timed out |
2090 ms |
1236 KB |
Time limit exceeded |
86 |
Halted |
0 ms |
0 KB |
- |