#include <bits/stdc++.h>
#include "peru.h"
using namespace std;
const int kN = 2e6 + 5e5;
const int base = 23;
const int mod = 1e9 + 7;
int a[1 + kN], stk[1 + kN], prv[1 + kN], nxt[1 + kN], dq[kN], dqMax[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[stk[vf]] <= a[i]) { /// <=
nxt[stk[vf]] = i;
vf -= 1;
}
prv[i] = stk[vf];
stk[++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 |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
15 |
Correct |
17 ms |
9988 KB |
Output is correct |
16 |
Correct |
16 ms |
10068 KB |
Output is correct |
17 |
Correct |
18 ms |
10368 KB |
Output is correct |
18 |
Correct |
25 ms |
9772 KB |
Output is correct |
19 |
Correct |
25 ms |
9800 KB |
Output is correct |
20 |
Correct |
25 ms |
9860 KB |
Output is correct |
21 |
Correct |
17 ms |
10012 KB |
Output is correct |
22 |
Correct |
17 ms |
10328 KB |
Output is correct |
23 |
Correct |
19 ms |
10324 KB |
Output is correct |
24 |
Correct |
20 ms |
10660 KB |
Output is correct |
25 |
Correct |
17 ms |
10736 KB |
Output is correct |
26 |
Correct |
16 ms |
10140 KB |
Output is correct |
27 |
Correct |
17 ms |
9940 KB |
Output is correct |
28 |
Correct |
17 ms |
9884 KB |
Output is correct |
29 |
Correct |
16 ms |
9868 KB |
Output is correct |
30 |
Correct |
18 ms |
10036 KB |
Output is correct |
31 |
Correct |
17 ms |
10420 KB |
Output is correct |
32 |
Correct |
16 ms |
9940 KB |
Output is correct |
33 |
Correct |
16 ms |
10452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
9988 KB |
Output is correct |
2 |
Correct |
16 ms |
10068 KB |
Output is correct |
3 |
Correct |
18 ms |
10368 KB |
Output is correct |
4 |
Correct |
25 ms |
9772 KB |
Output is correct |
5 |
Correct |
25 ms |
9800 KB |
Output is correct |
6 |
Correct |
25 ms |
9860 KB |
Output is correct |
7 |
Correct |
17 ms |
10012 KB |
Output is correct |
8 |
Correct |
17 ms |
10328 KB |
Output is correct |
9 |
Correct |
19 ms |
10324 KB |
Output is correct |
10 |
Correct |
20 ms |
10660 KB |
Output is correct |
11 |
Correct |
17 ms |
10736 KB |
Output is correct |
12 |
Correct |
16 ms |
10140 KB |
Output is correct |
13 |
Correct |
17 ms |
9940 KB |
Output is correct |
14 |
Correct |
17 ms |
9884 KB |
Output is correct |
15 |
Correct |
16 ms |
9868 KB |
Output is correct |
16 |
Correct |
18 ms |
10036 KB |
Output is correct |
17 |
Correct |
17 ms |
10420 KB |
Output is correct |
18 |
Correct |
16 ms |
9940 KB |
Output is correct |
19 |
Correct |
16 ms |
10452 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
102 ms |
62060 KB |
Output is correct |
35 |
Correct |
98 ms |
61108 KB |
Output is correct |
36 |
Correct |
102 ms |
61012 KB |
Output is correct |
37 |
Correct |
100 ms |
62248 KB |
Output is correct |
38 |
Correct |
110 ms |
61356 KB |
Output is correct |
39 |
Correct |
104 ms |
61412 KB |
Output is correct |
40 |
Correct |
101 ms |
63564 KB |
Output is correct |
41 |
Correct |
102 ms |
63992 KB |
Output is correct |
42 |
Correct |
100 ms |
62564 KB |
Output is correct |
43 |
Correct |
38 ms |
24124 KB |
Output is correct |
44 |
Correct |
90 ms |
35708 KB |
Output is correct |
45 |
Correct |
91 ms |
35660 KB |
Output is correct |
46 |
Correct |
91 ms |
35680 KB |
Output is correct |
47 |
Correct |
104 ms |
61256 KB |
Output is correct |
48 |
Correct |
101 ms |
61792 KB |
Output is correct |
49 |
Correct |
101 ms |
61248 KB |
Output is correct |
50 |
Correct |
101 ms |
60676 KB |
Output is correct |
51 |
Correct |
103 ms |
61004 KB |
Output is correct |
52 |
Correct |
103 ms |
61120 KB |
Output is correct |
53 |
Correct |
116 ms |
60588 KB |
Output is correct |
54 |
Correct |
104 ms |
63860 KB |
Output is correct |
55 |
Correct |
105 ms |
60224 KB |
Output is correct |
56 |
Correct |
101 ms |
60216 KB |
Output is correct |
57 |
Correct |
102 ms |
60064 KB |
Output is correct |
58 |
Correct |
100 ms |
60900 KB |
Output is correct |
59 |
Correct |
98 ms |
61004 KB |
Output is correct |
60 |
Correct |
104 ms |
61132 KB |
Output is correct |
61 |
Correct |
109 ms |
66308 KB |
Output is correct |
62 |
Correct |
102 ms |
63436 KB |
Output is correct |
63 |
Correct |
106 ms |
64404 KB |
Output is correct |