#include "peru.h"
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint INF = 1e18;
const int MAX = 2.5e6 + 5;
int N, K;
int A[MAX];
lint dp[MAX];
int left_geq[MAX];
int minqhead = 0;
int minqtail = 0;
pair<int, long long> minq[MAX];
int minstsz = 0;
int minst[MAX];
void InsertMinQueue(int p, long long x) {
while (minqhead < minqtail && minq[minqtail - 1].second >= x) {
minqtail--;
}
minq[minqtail++] = {p, x};
}
void PopQueue(int p) {
while (minqhead < minqtail && minq[minqhead].first < p) {
minqhead++;
}
}
int cost[MAX]; // cost[i] = maximum value in [i - K + 1, i]
int at_most[MAX]; // at_most[i] = farthest segment for current subtree
vector<int> adj[MAX]; // adjacency list
void Dfs(int u, lint alt_dp) { // subtrees form a contiguous segment, so when we DFS we visit them in order 0, 1, 2, ..
PopQueue(u - K);
dp[u] = min({alt_dp, dp[max(u - K, 0)] + cost[u], minqhead == minqtail ? INF : minq[minqhead].second});
reverse(begin(adj[u]), end(adj[u]));
assert(is_sorted(begin(adj[u]), end(adj[u])));
for (auto v : adj[u]) {
if (u + K < at_most[v]) {
InsertMinQueue(u, dp[u] + A[v]);
// Note that, nxt_v has distance > K to u, so dp[u] wouldn't exist anymore in minQueue
Dfs(v, alt_dp);
} else {
Dfs(v, min(alt_dp, dp[u] + A[v]));
}
}
}
int solve(int n, int k, int* v) {
N = n, K = k;
for (int i = 0; i < N; i++) {
A[i + 1] = v[i];
}
{ // Compute left_geq[i] = greatest j < i, s.t. V[j] >= V[i]
for (int i = 1; i <= N; i++) {
while (minstsz > 0 && A[minst[minstsz - 1]] < A[i]) {
minstsz--;
}
left_geq[i] = minstsz == 0 ? 0 : minst[minstsz - 1];
minst[minstsz++] = i;
}
minstsz = 0;
}
{ // Compute cost[i]
minq[minqtail++] = {0, 0}; // for maxQueue
for (int i = 1; i <= N; i++) {
while (minqhead < minqtail && minq[minqhead].first <= i - K) {
minqhead++;
}
while (minqhead < minqtail && minq[minqtail - 1].second <= A[i]) {
minqtail--;
}
minq[minqtail++] = {i, A[i]};
cost[i] = minq[minqhead].second;
}
minqhead = minqtail = 0;
}
for (int i = N; i >= 1; i--) {
int p = left_geq[i];
adj[p].emplace_back(i);
at_most[i] = max(at_most[i], i);
at_most[p] = max(at_most[p], at_most[i]);
}
Dfs(0, INF);
int ans = 0;
for (int i = 1; i <= N; i++) {
ans = (1ll * ans * 23 + dp[i]) % int(1e9 + 7);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
59148 KB |
Output is correct |
2 |
Correct |
35 ms |
59224 KB |
Output is correct |
3 |
Correct |
35 ms |
59164 KB |
Output is correct |
4 |
Correct |
28 ms |
59088 KB |
Output is correct |
5 |
Correct |
34 ms |
59060 KB |
Output is correct |
6 |
Correct |
31 ms |
59044 KB |
Output is correct |
7 |
Correct |
29 ms |
59220 KB |
Output is correct |
8 |
Correct |
33 ms |
59220 KB |
Output is correct |
9 |
Correct |
35 ms |
59220 KB |
Output is correct |
10 |
Correct |
32 ms |
59144 KB |
Output is correct |
11 |
Correct |
29 ms |
59104 KB |
Output is correct |
12 |
Correct |
37 ms |
59116 KB |
Output is correct |
13 |
Correct |
36 ms |
59172 KB |
Output is correct |
14 |
Correct |
32 ms |
59148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
59148 KB |
Output is correct |
2 |
Correct |
35 ms |
59224 KB |
Output is correct |
3 |
Correct |
35 ms |
59164 KB |
Output is correct |
4 |
Correct |
28 ms |
59088 KB |
Output is correct |
5 |
Correct |
34 ms |
59060 KB |
Output is correct |
6 |
Correct |
31 ms |
59044 KB |
Output is correct |
7 |
Correct |
29 ms |
59220 KB |
Output is correct |
8 |
Correct |
33 ms |
59220 KB |
Output is correct |
9 |
Correct |
35 ms |
59220 KB |
Output is correct |
10 |
Correct |
32 ms |
59144 KB |
Output is correct |
11 |
Correct |
29 ms |
59104 KB |
Output is correct |
12 |
Correct |
37 ms |
59116 KB |
Output is correct |
13 |
Correct |
36 ms |
59172 KB |
Output is correct |
14 |
Correct |
32 ms |
59148 KB |
Output is correct |
15 |
Correct |
70 ms |
83996 KB |
Output is correct |
16 |
Correct |
83 ms |
84528 KB |
Output is correct |
17 |
Correct |
81 ms |
86188 KB |
Output is correct |
18 |
Correct |
82 ms |
77440 KB |
Output is correct |
19 |
Correct |
90 ms |
77352 KB |
Output is correct |
20 |
Correct |
85 ms |
77396 KB |
Output is correct |
21 |
Correct |
68 ms |
84988 KB |
Output is correct |
22 |
Correct |
71 ms |
86860 KB |
Output is correct |
23 |
Correct |
73 ms |
86836 KB |
Output is correct |
24 |
Correct |
79 ms |
87560 KB |
Output is correct |
25 |
Correct |
77 ms |
89076 KB |
Output is correct |
26 |
Correct |
80 ms |
84892 KB |
Output is correct |
27 |
Correct |
70 ms |
84312 KB |
Output is correct |
28 |
Correct |
90 ms |
84672 KB |
Output is correct |
29 |
Correct |
93 ms |
84372 KB |
Output is correct |
30 |
Correct |
89 ms |
85068 KB |
Output is correct |
31 |
Correct |
73 ms |
86328 KB |
Output is correct |
32 |
Correct |
69 ms |
85012 KB |
Output is correct |
33 |
Correct |
72 ms |
87244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
83996 KB |
Output is correct |
2 |
Correct |
83 ms |
84528 KB |
Output is correct |
3 |
Correct |
81 ms |
86188 KB |
Output is correct |
4 |
Correct |
82 ms |
77440 KB |
Output is correct |
5 |
Correct |
90 ms |
77352 KB |
Output is correct |
6 |
Correct |
85 ms |
77396 KB |
Output is correct |
7 |
Correct |
68 ms |
84988 KB |
Output is correct |
8 |
Correct |
71 ms |
86860 KB |
Output is correct |
9 |
Correct |
73 ms |
86836 KB |
Output is correct |
10 |
Correct |
79 ms |
87560 KB |
Output is correct |
11 |
Correct |
77 ms |
89076 KB |
Output is correct |
12 |
Correct |
80 ms |
84892 KB |
Output is correct |
13 |
Correct |
70 ms |
84312 KB |
Output is correct |
14 |
Correct |
90 ms |
84672 KB |
Output is correct |
15 |
Correct |
93 ms |
84372 KB |
Output is correct |
16 |
Correct |
89 ms |
85068 KB |
Output is correct |
17 |
Correct |
73 ms |
86328 KB |
Output is correct |
18 |
Correct |
69 ms |
85012 KB |
Output is correct |
19 |
Correct |
72 ms |
87244 KB |
Output is correct |
20 |
Correct |
35 ms |
59148 KB |
Output is correct |
21 |
Correct |
35 ms |
59224 KB |
Output is correct |
22 |
Correct |
35 ms |
59164 KB |
Output is correct |
23 |
Correct |
28 ms |
59088 KB |
Output is correct |
24 |
Correct |
34 ms |
59060 KB |
Output is correct |
25 |
Correct |
31 ms |
59044 KB |
Output is correct |
26 |
Correct |
29 ms |
59220 KB |
Output is correct |
27 |
Correct |
33 ms |
59220 KB |
Output is correct |
28 |
Correct |
35 ms |
59220 KB |
Output is correct |
29 |
Correct |
32 ms |
59144 KB |
Output is correct |
30 |
Correct |
29 ms |
59104 KB |
Output is correct |
31 |
Correct |
37 ms |
59116 KB |
Output is correct |
32 |
Correct |
36 ms |
59172 KB |
Output is correct |
33 |
Correct |
32 ms |
59148 KB |
Output is correct |
34 |
Correct |
264 ms |
217264 KB |
Output is correct |
35 |
Correct |
266 ms |
214580 KB |
Output is correct |
36 |
Correct |
254 ms |
211748 KB |
Output is correct |
37 |
Correct |
287 ms |
217740 KB |
Output is correct |
38 |
Correct |
287 ms |
213844 KB |
Output is correct |
39 |
Correct |
297 ms |
215684 KB |
Output is correct |
40 |
Correct |
274 ms |
227540 KB |
Output is correct |
41 |
Correct |
302 ms |
227400 KB |
Output is correct |
42 |
Correct |
269 ms |
221376 KB |
Output is correct |
43 |
Correct |
130 ms |
120180 KB |
Output is correct |
44 |
Correct |
233 ms |
125280 KB |
Output is correct |
45 |
Correct |
234 ms |
125192 KB |
Output is correct |
46 |
Correct |
242 ms |
125268 KB |
Output is correct |
47 |
Correct |
278 ms |
214708 KB |
Output is correct |
48 |
Correct |
280 ms |
219344 KB |
Output is correct |
49 |
Correct |
258 ms |
215900 KB |
Output is correct |
50 |
Correct |
261 ms |
210576 KB |
Output is correct |
51 |
Correct |
285 ms |
214276 KB |
Output is correct |
52 |
Correct |
278 ms |
215016 KB |
Output is correct |
53 |
Correct |
289 ms |
210684 KB |
Output is correct |
54 |
Correct |
300 ms |
223488 KB |
Output is correct |
55 |
Correct |
279 ms |
206584 KB |
Output is correct |
56 |
Correct |
249 ms |
206428 KB |
Output is correct |
57 |
Correct |
252 ms |
206540 KB |
Output is correct |
58 |
Correct |
281 ms |
211964 KB |
Output is correct |
59 |
Correct |
264 ms |
213356 KB |
Output is correct |
60 |
Correct |
273 ms |
211912 KB |
Output is correct |
61 |
Correct |
311 ms |
244808 KB |
Output is correct |
62 |
Correct |
292 ms |
229244 KB |
Output is correct |
63 |
Correct |
257 ms |
229352 KB |
Output is correct |