#include <stdio.h>
#include <string.h>
#define N 8000
#define M (N * 4)
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int ll[M], rr[M], xx[M], yy[M];
int compare_y(int h1, int h2) {
return yy[h2] - yy[h1];
}
int compare_r(int h1, int h2) {
return rr[h2] - rr[h1];
}
int (*compare)(int, int);
void sort(int *hh, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = compare(hh[j], h);
if (c == 0)
j++;
else if (c < 0) {
tmp = hh[i], hh[i] = hh[j], hh[j] = tmp;
i++, j++;
} else {
k--;
tmp = hh[j], hh[j] = hh[k], hh[k] = tmp;
}
}
sort(hh, l, i);
l = k;
}
}
int ft[N * 2 + 1];
void update(int i, int n, int x) {
while (i < n) {
ft[i] = min(ft[i], x);
i |= i + 1;
}
}
int query(int i) {
int x = INF;
while (i >= 0) {
x = min(x, ft[i]);
i &= i + 1, i--;
}
return x;
}
int main() {
static int aa[N], ii[N], hh[M], jj[2][2][N], tt[N];
int n, m, k, h, h_, i, j, a, u, v, l, x, tmp;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &aa[i]), aa[i]--;
ii[aa[i]] = i;
}
m = 0;
ll[m] = 0, rr[m] = n - 1, xx[m] = 0, yy[m] = n - 1, m++;
k = 0;
while (1) {
for (u = 0; u < 2; u++) {
for (h = 0; h < m; h++)
hh[h] = h;
for (v = 0; v < 2; v++) {
memset(jj[u][v], 0x3f, n * sizeof *jj[u][v]);
compare = compare_y, sort(hh, 0, m);
memset(ft, 0x3f, (n * 2 + 1) * sizeof *ft);
for (a = n - 1, h = 0; a >= 0; a--) {
while (h < m && yy[h_ = hh[h]] >= a)
update(yy[h_] - rr[h_] + n, n * 2 + 1, ll[h_]), h++;
l = query(a - ii[a] + n);
if (l != INF)
jj[u][v][ii[a]] = min(jj[u][v][ii[a]], l);
}
compare = compare_r, sort(hh, 0, m);
memset(ft, 0x3f, (n * 2 + 1) * sizeof *ft);
for (i = n - 1, h = 0; i >= 0; i--) {
while (h < m && rr[h_ = hh[h]] >= i)
update(rr[h_] - yy[h_] + n, n * 2 + 1, xx[h_]), h++;
x = query(i - aa[i] + n);
if (x != INF)
jj[u][v][i] = min(jj[u][v][i], -aa[i] + i + x);
}
for (i = 0; i < n; i++) {
aa[i] = n - 1 - aa[i];
ii[aa[i]] = i;
}
for (h = 0; h < m; h++)
tmp = n - 1 - xx[h], xx[h] = n - 1 - yy[h], yy[h] = tmp;
}
for (a = 0; a < n; a++) {
ii[a] = n - 1 - ii[a];
aa[ii[a]] = a;
}
for (h = 0; h < m; h++)
tmp = n - 1 - ll[h], ll[h] = n - 1 - rr[h], rr[h] = tmp;
}
for (v = 0; v < 2; v++) {
for (i = 0; i < n; i++)
tt[i] = jj[1][v][n - 1 - i] == INF ? -INF : n - 1 - jj[1][v][n - 1 - i];
memcpy(jj[1][v], tt, n * sizeof *tt);
}
m = 0;
for (i = 0; i < n; i++) {
if ((j = jj[0][0][i]) <= i)
ll[m] = j, rr[m] = i - 1, xx[m] = aa[i] - i + j, yy[m] = aa[i] - 1, m++;
if ((j = jj[0][1][i]) <= i)
ll[m] = j, rr[m] = i - 1, xx[m] = aa[i] + 1, yy[m] = aa[i] + i - j, m++;
if ((j = jj[1][0][i]) >= i)
ll[m] = i + 1, rr[m] = j, xx[m] = aa[i] - j + i, yy[m] = aa[i] - 1, m++;
if ((j = jj[1][1][i]) >= i)
ll[m] = i + 1, rr[m] = j, xx[m] = aa[i] + 1, yy[m] = aa[i] + j - i, m++;
}
if (m == 0) {
printf("%d\n", n - k);
return 0;
}
k++;
}
return 0;
}
Compilation message
giraffes.c: In function 'main':
giraffes.c:74:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
giraffes.c:76:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf("%d", &aa[i]), aa[i]--;
| ^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
292 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
292 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
292 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
292 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
292 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
4 ms |
340 KB |
Output is correct |
23 |
Correct |
13 ms |
340 KB |
Output is correct |
24 |
Correct |
20 ms |
368 KB |
Output is correct |
25 |
Correct |
26 ms |
340 KB |
Output is correct |
26 |
Correct |
28 ms |
292 KB |
Output is correct |
27 |
Correct |
29 ms |
360 KB |
Output is correct |
28 |
Correct |
28 ms |
340 KB |
Output is correct |
29 |
Correct |
31 ms |
340 KB |
Output is correct |
30 |
Correct |
29 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
292 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
292 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
4 ms |
340 KB |
Output is correct |
23 |
Correct |
13 ms |
340 KB |
Output is correct |
24 |
Correct |
20 ms |
368 KB |
Output is correct |
25 |
Correct |
26 ms |
340 KB |
Output is correct |
26 |
Correct |
28 ms |
292 KB |
Output is correct |
27 |
Correct |
29 ms |
360 KB |
Output is correct |
28 |
Correct |
28 ms |
340 KB |
Output is correct |
29 |
Correct |
31 ms |
340 KB |
Output is correct |
30 |
Correct |
29 ms |
340 KB |
Output is correct |
31 |
Correct |
835 ms |
584 KB |
Output is correct |
32 |
Correct |
3743 ms |
1000 KB |
Output is correct |
33 |
Correct |
6263 ms |
1208 KB |
Output is correct |
34 |
Correct |
6363 ms |
1204 KB |
Output is correct |
35 |
Correct |
6293 ms |
1204 KB |
Output is correct |
36 |
Correct |
6433 ms |
1204 KB |
Output is correct |
37 |
Correct |
6409 ms |
1204 KB |
Output is correct |
38 |
Correct |
6496 ms |
1204 KB |
Output is correct |
39 |
Correct |
6401 ms |
1204 KB |
Output is correct |
40 |
Correct |
6349 ms |
1208 KB |
Output is correct |