This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* upsolve after reading spoiler */
#include <stdio.h>
#include <stdlib.h>
#define N 2000000
int max(int a, int b) { return a > b ? a : b; }
void append(int **ej, int *eo, int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
int main() {
static int *ej[N], eo[N], *fj[N], fo[N];
static int aa[N], qu[N], pp[N], dd[N], jj[N];
static char passive[N];
int n, k, t, i, j, d, o, cnt, ans;
scanf("%d%d%d", &n, &k, &t);
for (i = 0, j = -1; i < n; i++) {
scanf("%d", &aa[i]);
j = max(j, i + t - aa[i]);
if (aa[i] > t && j >= i)
passive[i] = 1;
}
cnt = 0, ans = n;
for (i = n - 1; i >= 0; i--)
if (passive[i]) {
pp[i] = cnt == 0 ? -1 : qu[cnt - 1];
qu[cnt++] = i;
} else {
pp[i] = -2;
if (aa[i] > t)
ans--;
else
while (cnt && qu[cnt - 1] <= i + t - aa[i])
cnt--;
}
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (i = 0; i < n; i++)
if (pp[i] >= 0)
append(ej, eo, pp[i], i);
for (i = 0; i < n; i++)
for (o = eo[i]; o--; ) {
j = ej[i][o];
if (dd[i] < dd[j] + 1)
dd[i] = dd[j] + 1, jj[i] = j;
}
for (i = 0; i < n; i++)
fj[i] = (int *) malloc(2 * sizeof *fj[i]);
for (i = 0; i < n; i++)
if (pp[i] == -1)
append(fj, fo, dd[i], i);
d = n - 1;
while (k--) {
while (d >= 0 && fo[d] == 0)
d--;
if (d < 0)
break;
i = fj[d][--fo[d]], ans -= d + 1;
while (dd[i] > 0) {
for (o = eo[i]; o--; ) {
j = ej[i][o];
if (j != jj[i])
append(fj, fo, dd[j], j);
}
i = jj[i];
}
}
printf("%d\n", ans);
return 0;
}
Compilation message (stderr)
prison.c: In function 'append':
prison.c:12:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
12 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
prison.c: In function 'main':
prison.c:23:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | scanf("%d%d%d", &n, &k, &t);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
prison.c:25:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |