#include <stdio.h>
#include <string.h>
#define N 100000
#define L 16 /* L = floor(log2(N)) */
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int main() {
static int aa[N], dp[N][L + 1], dq[N][L + 1], dr[N + 1];
int n, i, j, l, a;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
for (i = 0; i < n; i++)
if (aa[i] > L + 1) {
printf("-1\n");
return 0;
}
for (l = 0; 1 << l <= n; l++) {
if (l == 0)
for (i = 0; i < n; i++)
memset(dp[i], 0x3f, (L + 1) * sizeof *dp[i]), dp[i][aa[i]] = 0;
else
for (i = 0; i + (1 << l) <= n; i++)
for (a = 0; a <= L; a++)
dp[i][a] = min(dp[i][a] + dp[i + (1 << l - 1)][a], INF);
for (i = 0; i < n; i++)
for (a = 0; a < L; a++)
dp[i][a] = min(dp[i][a], dp[i][a + 1] + 1);
for (i = 0; i + (1 << l) <= n; i++)
dq[i][l] = dp[i][0];
}
memset(dr, 0x3f, (n + 1) * sizeof *dr), dr[0] = 0;
for (i = 0; i <= n; i++)
for (l = 0; (j = i + (1 << l)) <= n; l++)
dr[j] = min(dr[j], dr[i] + dq[i][l]);
printf("%d\n", dr[n] == INF ? -1 : dr[n]);
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:29:47: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
29 | dp[i][a] = min(dp[i][a] + dp[i + (1 << l - 1)][a], INF);
| ~~^~~
Main.c:14:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:16:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
296 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
5 ms |
1716 KB |
Output is correct |
21 |
Correct |
5 ms |
1620 KB |
Output is correct |
22 |
Correct |
73 ms |
14596 KB |
Output is correct |
23 |
Correct |
7 ms |
980 KB |
Output is correct |
24 |
Correct |
65 ms |
14552 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 |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
5 ms |
1720 KB |
Output is correct |
30 |
Correct |
6 ms |
1696 KB |
Output is correct |
31 |
Correct |
8 ms |
1688 KB |
Output is correct |
32 |
Correct |
5 ms |
1620 KB |
Output is correct |
33 |
Correct |
64 ms |
14548 KB |
Output is correct |
34 |
Correct |
65 ms |
14536 KB |
Output is correct |
35 |
Correct |
65 ms |
14640 KB |
Output is correct |
36 |
Correct |
64 ms |
14552 KB |
Output is correct |
37 |
Correct |
64 ms |
14644 KB |
Output is correct |
38 |
Correct |
74 ms |
14560 KB |
Output is correct |
39 |
Correct |
69 ms |
14532 KB |
Output is correct |
40 |
Correct |
64 ms |
14500 KB |
Output is correct |
41 |
Correct |
66 ms |
14544 KB |
Output is correct |
42 |
Correct |
80 ms |
14644 KB |
Output is correct |
43 |
Correct |
64 ms |
14548 KB |
Output is correct |
44 |
Correct |
63 ms |
14552 KB |
Output is correct |
45 |
Correct |
65 ms |
14552 KB |
Output is correct |
46 |
Correct |
64 ms |
14548 KB |
Output is correct |
47 |
Correct |
44 ms |
9812 KB |
Output is correct |
48 |
Correct |
48 ms |
11368 KB |
Output is correct |
49 |
Correct |
56 ms |
12904 KB |
Output is correct |
50 |
Correct |
64 ms |
14444 KB |
Output is correct |
51 |
Correct |
8 ms |
1728 KB |
Output is correct |
52 |
Correct |
81 ms |
14512 KB |
Output is correct |