#include "peru.h"
#include <bits/stdc++.h>
using namespace std;
const int md = 1e9 + 7;
int add(int x, int y) {
return x + y < md ? x + y : x + y - md;
}
int mul(int x, int y) {
return x * 1LL * y % md;
}
int solve(int n, int k, int* v) {
vector<int> ft(n);
deque<int> dq;
for (int i = 0; i < n; i++) {
while (!dq.empty() && v[i] >= v[dq.back()]) {
dq.pop_back();
}
while (!dq.empty() && i - dq.front() + 1 > k) {
dq.pop_front();
}
dq.push_back(i);
ft[i] = v[dq.front()];
}
const long long inf = (long long) 1e18;
vector<long long> dp(n, inf);
multiset<pair<long long, int>> st;
vector<array<int, 3>> stk;
for (int i = 0; i < n; i++) {
int border = i;
while (!stk.empty() && v[i] > stk.back()[0]) {
border = stk.back()[1];
st.erase({(border == 0 ? 0LL : dp[border - 1]) + stk.back()[0], border});
stk.pop_back();
}
stk.push_back({v[i], border, i});
st.emplace((border == 0 ? 0LL : dp[border - 1]) + v[i], border);
while (!st.empty() && i - st.begin()->second + 1 > k) {
st.erase(st.begin());
}
if (!st.empty()) {
dp[i] = min(dp[i], st.begin()->first);
}
int j = max(0, i - k + 1);
dp[i] = min(dp[i], (j == 0 ? 0LL : dp[j - 1]) + ft[i]);
}
int res = 0;
int pw = 1;
for (int i = n - 1; i >= 0; i--) {
dp[i] %= md;
res = add(res, mul(pw, dp[i]));
pw = mul(pw, 23);
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
59 ms |
7660 KB |
Output is correct |
16 |
Correct |
48 ms |
7768 KB |
Output is correct |
17 |
Correct |
53 ms |
7988 KB |
Output is correct |
18 |
Correct |
42 ms |
7508 KB |
Output is correct |
19 |
Correct |
36 ms |
7512 KB |
Output is correct |
20 |
Correct |
38 ms |
7260 KB |
Output is correct |
21 |
Correct |
50 ms |
7772 KB |
Output is correct |
22 |
Correct |
53 ms |
8284 KB |
Output is correct |
23 |
Correct |
54 ms |
8284 KB |
Output is correct |
24 |
Correct |
51 ms |
11632 KB |
Output is correct |
25 |
Correct |
54 ms |
12504 KB |
Output is correct |
26 |
Correct |
49 ms |
11356 KB |
Output is correct |
27 |
Correct |
52 ms |
11600 KB |
Output is correct |
28 |
Correct |
54 ms |
11856 KB |
Output is correct |
29 |
Correct |
50 ms |
11832 KB |
Output is correct |
30 |
Correct |
54 ms |
11592 KB |
Output is correct |
31 |
Correct |
48 ms |
11500 KB |
Output is correct |
32 |
Correct |
53 ms |
11864 KB |
Output is correct |
33 |
Correct |
47 ms |
11608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
7660 KB |
Output is correct |
2 |
Correct |
48 ms |
7768 KB |
Output is correct |
3 |
Correct |
53 ms |
7988 KB |
Output is correct |
4 |
Correct |
42 ms |
7508 KB |
Output is correct |
5 |
Correct |
36 ms |
7512 KB |
Output is correct |
6 |
Correct |
38 ms |
7260 KB |
Output is correct |
7 |
Correct |
50 ms |
7772 KB |
Output is correct |
8 |
Correct |
53 ms |
8284 KB |
Output is correct |
9 |
Correct |
54 ms |
8284 KB |
Output is correct |
10 |
Correct |
51 ms |
11632 KB |
Output is correct |
11 |
Correct |
54 ms |
12504 KB |
Output is correct |
12 |
Correct |
49 ms |
11356 KB |
Output is correct |
13 |
Correct |
52 ms |
11600 KB |
Output is correct |
14 |
Correct |
54 ms |
11856 KB |
Output is correct |
15 |
Correct |
50 ms |
11832 KB |
Output is correct |
16 |
Correct |
54 ms |
11592 KB |
Output is correct |
17 |
Correct |
48 ms |
11500 KB |
Output is correct |
18 |
Correct |
53 ms |
11864 KB |
Output is correct |
19 |
Correct |
47 ms |
11608 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
325 ms |
61688 KB |
Output is correct |
35 |
Correct |
324 ms |
65604 KB |
Output is correct |
36 |
Correct |
355 ms |
65060 KB |
Output is correct |
37 |
Correct |
335 ms |
64668 KB |
Output is correct |
38 |
Correct |
334 ms |
64680 KB |
Output is correct |
39 |
Correct |
348 ms |
65712 KB |
Output is correct |
40 |
Correct |
366 ms |
68020 KB |
Output is correct |
41 |
Correct |
346 ms |
65548 KB |
Output is correct |
42 |
Correct |
357 ms |
65808 KB |
Output is correct |
43 |
Correct |
125 ms |
26964 KB |
Output is correct |
44 |
Correct |
159 ms |
39788 KB |
Output is correct |
45 |
Correct |
139 ms |
39804 KB |
Output is correct |
46 |
Correct |
137 ms |
39660 KB |
Output is correct |
47 |
Correct |
335 ms |
67828 KB |
Output is correct |
48 |
Correct |
352 ms |
68296 KB |
Output is correct |
49 |
Correct |
350 ms |
69136 KB |
Output is correct |
50 |
Correct |
319 ms |
68392 KB |
Output is correct |
51 |
Correct |
334 ms |
70412 KB |
Output is correct |
52 |
Correct |
329 ms |
72968 KB |
Output is correct |
53 |
Correct |
338 ms |
68884 KB |
Output is correct |
54 |
Correct |
347 ms |
64296 KB |
Output is correct |
55 |
Correct |
309 ms |
63520 KB |
Output is correct |
56 |
Correct |
314 ms |
63316 KB |
Output is correct |
57 |
Correct |
311 ms |
63828 KB |
Output is correct |
58 |
Correct |
334 ms |
64428 KB |
Output is correct |
59 |
Correct |
322 ms |
65292 KB |
Output is correct |
60 |
Correct |
320 ms |
64656 KB |
Output is correct |
61 |
Correct |
378 ms |
74292 KB |
Output is correct |
62 |
Correct |
370 ms |
70668 KB |
Output is correct |
63 |
Correct |
371 ms |
67744 KB |
Output is correct |