This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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]);
| ^~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |