#include <stdio.h>
#define N 500000
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int main() {
static int aa[N], ii1[N], ii2[N], kkp1[N + 1], kkp2[N + 1], kkq1[N + 1], kkq2[N + 1];
static long long vvp[N + 1], vvq[N + 1];
int n, n0, n1, n2, i, l1, l2, k, k1, k2;
long long v, ans;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &aa[i]);
if (i % 2 != 0)
aa[i] = -aa[i];
}
n1 = 0, n2 = 0;
for (i = 0; i < n; i++)
if (aa[i] == -1)
ii1[n1++] = i;
else if (aa[i] == 1)
ii2[n2++] = i;
n0 = n - n1 - n2;
kkp1[0] = k1 = 0, kkp2[0] = k2 = 0, vvp[0] = v = 0;
for (k = 1, l1 = 0, l2 = 0; k <= n1 + n2; k++) {
if ((k - 1) % 2 == 0) {
if (k1 < n1) {
i = ii1[k1];
while (l2 < n2 && ii2[l2] < i)
l2++;
v += i - k1 - min(l2, k2);
}
k1++;
} else {
if (k2 < n2) {
i = ii2[k2];
while (l1 < n1 && ii1[l1] < i)
l1++;
v += i - k2 - min(l1, k1);
}
k2++;
}
kkp1[k] = k1, kkp2[k] = k2, vvp[k] = v;
}
kkq1[n1 + n2] = k1 = n1, kkq2[n1 + n2] = k2 = n2, vvq[n1 + n2] = v = 0;
for (k = n1 + n2 - 1, l1 = n1, l2 = n2; k >= 0; k--) {
if ((n0 + k) % 2 != 0) {
k1--;
if (k1 >= 0) {
i = ii1[k1];
while (l2 > 0 && ii2[l2 - 1] >= i)
l2--;
v += n0 - (i - k1 - l2) + max(l2 - k2, 0);
}
} else {
k2--;
if (k2 >= 0) {
i = ii2[k2];
while (l1 > 0 && ii1[l1 - 1] >= i)
l1--;
v += n0 - (i - k2 - l1) + max(l1 - k1, 0);
}
}
kkq1[k] = k1, kkq2[k] = k2, vvq[k] = v;
}
ans = INF;
for (k = 0; k <= n1 + n2; k++)
if (kkp1[k] == kkq1[k] && kkp2[k] == kkq2[k])
ans = min(ans, vvp[k] + vvq[k]);
if (ans == INF)
ans = -1;
printf("%lld\n", ans);
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:15:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:17:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
51 ms |
20652 KB |
Output is correct |
16 |
Correct |
54 ms |
19964 KB |
Output is correct |
17 |
Correct |
53 ms |
20096 KB |
Output is correct |
18 |
Correct |
49 ms |
20160 KB |
Output is correct |
19 |
Correct |
56 ms |
20724 KB |
Output is correct |
20 |
Correct |
40 ms |
20428 KB |
Output is correct |
21 |
Correct |
39 ms |
20924 KB |
Output is correct |
22 |
Correct |
50 ms |
20812 KB |
Output is correct |
23 |
Correct |
38 ms |
20076 KB |
Output is correct |
24 |
Correct |
41 ms |
20964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 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 |
0 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 |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
304 KB |
Output is correct |
27 |
Correct |
1 ms |
292 KB |
Output is correct |
28 |
Correct |
1 ms |
304 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
288 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
51 ms |
20652 KB |
Output is correct |
16 |
Correct |
54 ms |
19964 KB |
Output is correct |
17 |
Correct |
53 ms |
20096 KB |
Output is correct |
18 |
Correct |
49 ms |
20160 KB |
Output is correct |
19 |
Correct |
56 ms |
20724 KB |
Output is correct |
20 |
Correct |
40 ms |
20428 KB |
Output is correct |
21 |
Correct |
39 ms |
20924 KB |
Output is correct |
22 |
Correct |
50 ms |
20812 KB |
Output is correct |
23 |
Correct |
38 ms |
20076 KB |
Output is correct |
24 |
Correct |
41 ms |
20964 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
304 KB |
Output is correct |
38 |
Correct |
1 ms |
292 KB |
Output is correct |
39 |
Correct |
1 ms |
304 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
288 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
42 ms |
11636 KB |
Output is correct |
45 |
Correct |
51 ms |
11508 KB |
Output is correct |
46 |
Correct |
42 ms |
11976 KB |
Output is correct |
47 |
Correct |
53 ms |
11912 KB |
Output is correct |
48 |
Correct |
46 ms |
14924 KB |
Output is correct |
49 |
Correct |
53 ms |
21016 KB |
Output is correct |
50 |
Correct |
33 ms |
11288 KB |
Output is correct |
51 |
Correct |
32 ms |
12108 KB |
Output is correct |
52 |
Correct |
34 ms |
11416 KB |
Output is correct |
53 |
Correct |
32 ms |
12132 KB |
Output is correct |
54 |
Correct |
35 ms |
14224 KB |
Output is correct |
55 |
Correct |
36 ms |
14156 KB |
Output is correct |
56 |
Correct |
35 ms |
14972 KB |
Output is correct |
57 |
Correct |
29 ms |
2532 KB |
Output is correct |
58 |
Correct |
29 ms |
2584 KB |
Output is correct |
59 |
Correct |
31 ms |
2968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 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 |
0 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 |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
304 KB |
Output is correct |
27 |
Correct |
1 ms |
292 KB |
Output is correct |
28 |
Correct |
1 ms |
304 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
288 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
51 ms |
20652 KB |
Output is correct |
16 |
Correct |
54 ms |
19964 KB |
Output is correct |
17 |
Correct |
53 ms |
20096 KB |
Output is correct |
18 |
Correct |
49 ms |
20160 KB |
Output is correct |
19 |
Correct |
56 ms |
20724 KB |
Output is correct |
20 |
Correct |
40 ms |
20428 KB |
Output is correct |
21 |
Correct |
39 ms |
20924 KB |
Output is correct |
22 |
Correct |
50 ms |
20812 KB |
Output is correct |
23 |
Correct |
38 ms |
20076 KB |
Output is correct |
24 |
Correct |
41 ms |
20964 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
304 KB |
Output is correct |
38 |
Correct |
1 ms |
292 KB |
Output is correct |
39 |
Correct |
1 ms |
304 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
288 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
42 ms |
11636 KB |
Output is correct |
45 |
Correct |
51 ms |
11508 KB |
Output is correct |
46 |
Correct |
42 ms |
11976 KB |
Output is correct |
47 |
Correct |
53 ms |
11912 KB |
Output is correct |
48 |
Correct |
46 ms |
14924 KB |
Output is correct |
49 |
Correct |
53 ms |
21016 KB |
Output is correct |
50 |
Correct |
33 ms |
11288 KB |
Output is correct |
51 |
Correct |
32 ms |
12108 KB |
Output is correct |
52 |
Correct |
34 ms |
11416 KB |
Output is correct |
53 |
Correct |
32 ms |
12132 KB |
Output is correct |
54 |
Correct |
35 ms |
14224 KB |
Output is correct |
55 |
Correct |
36 ms |
14156 KB |
Output is correct |
56 |
Correct |
35 ms |
14972 KB |
Output is correct |
57 |
Correct |
29 ms |
2532 KB |
Output is correct |
58 |
Correct |
29 ms |
2584 KB |
Output is correct |
59 |
Correct |
31 ms |
2968 KB |
Output is correct |
60 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |