#include <bits/stdc++.h>
#include "peru.h"
using namespace std;
const int base = 23;
const int mod = 1e9 + 7;
const int kN = 2e6 + 5e5;
const int64_t INF = 1e18L;
int a[1 + kN], st[1 + kN], prv[1 + kN], nxt[1 + kN], dqMax[kN], dq[kN], stk[1 + kN];
int64_t dp[1 + kN];
void minSelf(int64_t &x, int64_t y) {
if (y < x) {
x = y;
}
}
int64_t cost(int index) {
return dp[prv[index]] + a[index];
}
int add(int x, int y) {
x += y;
if (x >= mod) {
x -= mod;
}
return x;
}
int mult(int x, int y) {
return (int64_t)x * y % mod;
}
int solve(int n, int k, int* v) {
for (int i = 1; i <= n; ++i) {
a[i] = v[i - 1];
}
int vf = 0;
for (int i = 1; i <= n; ++i) {
nxt[i] = n + 1;
while (vf && a[st[vf]] <= a[i]) { /// <=
nxt[st[vf]] = i;
vf -= 1;
}
prv[i] = st[vf];
st[++vf] = i;
}
int res = 0, l = 0, r = -1, lmax = 0, rmax = -1;
vf = 0;
for (int i = 1; i <= n; ++i) {
while (vf && a[stk[vf]] <= a[i]) { /// <=
vf -= 1;
}
if (nxt[i] - prv[i] >= k) {
while (l <= r && cost(i) <= cost(dq[r])) {
r -= 1;
}
dq[++r] = i;
} else if (vf == 0 || cost(i) < cost(stk[vf])) {
stk[++vf] = i;
}
while (l <= r && prv[dq[l]] <= i - k) {
l += 1;
}
while (lmax <= rmax && a[dqMax[rmax]] <= a[i]) {
rmax -= 1;
}
dqMax[++rmax] = i;
while (dqMax[lmax] <= i - k) {
lmax += 1;
}
if (i >= k) {
dp[i] = dp[i - k] + a[dqMax[lmax]];
} else {
dp[i] = a[dqMax[lmax]];
}
if (l <= r) {
minSelf(dp[i], cost(dq[l]));
}
if (vf) {
minSelf(dp[i], cost(stk[vf]));
}
res = add(mult(res, base), dp[i] % mod);
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
372 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 |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
372 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 |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
19 ms |
14020 KB |
Output is correct |
16 |
Correct |
18 ms |
14048 KB |
Output is correct |
17 |
Correct |
23 ms |
14408 KB |
Output is correct |
18 |
Correct |
26 ms |
13928 KB |
Output is correct |
19 |
Correct |
26 ms |
13884 KB |
Output is correct |
20 |
Correct |
27 ms |
13884 KB |
Output is correct |
21 |
Correct |
19 ms |
14188 KB |
Output is correct |
22 |
Correct |
18 ms |
14412 KB |
Output is correct |
23 |
Correct |
19 ms |
14412 KB |
Output is correct |
24 |
Correct |
19 ms |
14720 KB |
Output is correct |
25 |
Correct |
19 ms |
14908 KB |
Output is correct |
26 |
Correct |
18 ms |
14216 KB |
Output is correct |
27 |
Correct |
18 ms |
14048 KB |
Output is correct |
28 |
Correct |
18 ms |
14080 KB |
Output is correct |
29 |
Correct |
18 ms |
13980 KB |
Output is correct |
30 |
Correct |
24 ms |
14080 KB |
Output is correct |
31 |
Correct |
19 ms |
14500 KB |
Output is correct |
32 |
Correct |
19 ms |
14120 KB |
Output is correct |
33 |
Correct |
19 ms |
14640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
14020 KB |
Output is correct |
2 |
Correct |
18 ms |
14048 KB |
Output is correct |
3 |
Correct |
23 ms |
14408 KB |
Output is correct |
4 |
Correct |
26 ms |
13928 KB |
Output is correct |
5 |
Correct |
26 ms |
13884 KB |
Output is correct |
6 |
Correct |
27 ms |
13884 KB |
Output is correct |
7 |
Correct |
19 ms |
14188 KB |
Output is correct |
8 |
Correct |
18 ms |
14412 KB |
Output is correct |
9 |
Correct |
19 ms |
14412 KB |
Output is correct |
10 |
Correct |
19 ms |
14720 KB |
Output is correct |
11 |
Correct |
19 ms |
14908 KB |
Output is correct |
12 |
Correct |
18 ms |
14216 KB |
Output is correct |
13 |
Correct |
18 ms |
14048 KB |
Output is correct |
14 |
Correct |
18 ms |
14080 KB |
Output is correct |
15 |
Correct |
18 ms |
13980 KB |
Output is correct |
16 |
Correct |
24 ms |
14080 KB |
Output is correct |
17 |
Correct |
19 ms |
14500 KB |
Output is correct |
18 |
Correct |
19 ms |
14120 KB |
Output is correct |
19 |
Correct |
19 ms |
14640 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
372 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
94 ms |
61820 KB |
Output is correct |
35 |
Correct |
103 ms |
60752 KB |
Output is correct |
36 |
Correct |
93 ms |
60708 KB |
Output is correct |
37 |
Correct |
95 ms |
61908 KB |
Output is correct |
38 |
Correct |
95 ms |
61200 KB |
Output is correct |
39 |
Correct |
93 ms |
61256 KB |
Output is correct |
40 |
Correct |
103 ms |
63316 KB |
Output is correct |
41 |
Correct |
104 ms |
63620 KB |
Output is correct |
42 |
Correct |
97 ms |
62200 KB |
Output is correct |
43 |
Correct |
42 ms |
33504 KB |
Output is correct |
44 |
Correct |
102 ms |
50936 KB |
Output is correct |
45 |
Correct |
106 ms |
50992 KB |
Output is correct |
46 |
Correct |
99 ms |
51032 KB |
Output is correct |
47 |
Correct |
95 ms |
60868 KB |
Output is correct |
48 |
Correct |
95 ms |
61472 KB |
Output is correct |
49 |
Correct |
92 ms |
60816 KB |
Output is correct |
50 |
Correct |
95 ms |
60284 KB |
Output is correct |
51 |
Correct |
94 ms |
60692 KB |
Output is correct |
52 |
Correct |
96 ms |
60876 KB |
Output is correct |
53 |
Correct |
94 ms |
60384 KB |
Output is correct |
54 |
Correct |
96 ms |
63620 KB |
Output is correct |
55 |
Correct |
98 ms |
59848 KB |
Output is correct |
56 |
Correct |
98 ms |
59856 KB |
Output is correct |
57 |
Correct |
93 ms |
59824 KB |
Output is correct |
58 |
Correct |
98 ms |
60708 KB |
Output is correct |
59 |
Correct |
93 ms |
60656 KB |
Output is correct |
60 |
Correct |
91 ms |
60732 KB |
Output is correct |
61 |
Correct |
98 ms |
65756 KB |
Output is correct |
62 |
Correct |
96 ms |
62812 KB |
Output is correct |
63 |
Correct |
96 ms |
63660 KB |
Output is correct |