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 <stdlib.h>
#define N 1000000
int max(int a, int b) { return a > b ? a : b; }
int n, k;
long long xx[N], ss[N + 1]; int ll[N], rr[N];
int *ej[N], eo[N];
void append(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 qu[N], cnt; long long s_; int i_;
void dfs(int i) {
int o;
long long s;
qu[cnt++] = i;
s = ss[rr[i]] - ss[ll[qu[max(cnt - k, 0)]]];
if (s_ < s)
s_ = s, i_ = i;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
dfs(j);
}
cnt--;
}
int main() {
static int pp[N];
int i, j, l, r;
long long d;
scanf("%d%d%lld", &n, &k, &d);
for (i = 1; i < n; i++)
scanf("%lld", &xx[i]), xx[i] += xx[i - 1];
for (i = 1; i <= n; i++)
scanf("%lld", &ss[i]), ss[i] += ss[i - 1];
for (i = 0, l = 0, r = 0; i < n; i++) {
while (l < i && xx[i] - xx[l] > d)
l++;
while (r < n && xx[r] - xx[i] <= d)
r++;
ll[i] = l, rr[i] = r;
}
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
pp[0] = -1;
for (i = 0, j = 1; j < n; j++) {
while (rr[i] < ll[j])
i++;
pp[j] = i, append(i, j);
}
s_ = 0, i_ = -1, dfs(0);
cnt = 0;
while (cnt < k && i_ != -1) {
qu[cnt++] = i_;
if (ll[i_] == 0) /* the checker has a bug */
break;
i_ = pp[i_];
}
printf("%d\n", cnt);
while (cnt--)
printf("%d ", qu[cnt] + 1);
printf("\n");
return 0;
}
Compilation message (stderr)
SolarStorm.c: In function 'append':
SolarStorm.c:15:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
15 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
SolarStorm.c: In function 'main':
SolarStorm.c:43:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%d%d%lld", &n, &k, &d);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SolarStorm.c:45:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%lld", &xx[i]), xx[i] += xx[i - 1];
| ^~~~~~~~~~~~~~~~~~~~~
SolarStorm.c:47:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%lld", &ss[i]), ss[i] += ss[i - 1];
| ^~~~~~~~~~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |