#include "peru.h"
#include <string.h>
#define N 2500000
#define INF 0x3f3f3f3f3f3f3f3fLL
#define MD 1000000007
#define X 23
long long min(long long a, long long b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int solve(int n, int k, int *aa) {
static int qq[N + 1], pp[N + 1], qu[N + 1];
static long long dp[N + 1], dq[N + 1];
int cnt, i, j, l, r, ans;
long long x;
memset(dp, 0x3f, (n + 1) * sizeof *dp), dp[0] = 0;
for (l = 0; l <= n; l += k) {
r = min(l + k, n + 1);
if (l >= k) {
qq[l] = 0;
for (i = l - 1; i >= l - k; i--)
qq[i] = max(qq[i + 1], aa[i]);
pp[l] = 0;
for (i = l; i < r; i++)
pp[i + 1] = max(pp[i], aa[i]);
for (j = r - 1, i = j - k, x = INF; j >= l; j--) {
if (j - k >= 0 && i > j - k) {
if (qq[j - k] >= pp[j])
x = min(x, dp[j - k] + qq[j - k]);
else
i = j - k;
}
while (i < l && qq[i] >= pp[j])
x = min(x, dp[i] + qq[i]), i++;
dp[j] = min(x, i == l ? INF : dp[i] + pp[j]);
}
}
cnt = 0;
for (i = l; i < r; i++) {
if (cnt)
dp[i] = min(dp[i], dq[cnt - 1]);
if (i < r) {
while (cnt && aa[qu[cnt - 1]] <= aa[i])
cnt--;
qu[cnt] = i, dq[cnt] = cnt == 0 ? dp[l] + aa[i] : min(dq[cnt - 1], dp[qu[cnt - 1] + 1] + aa[i]), cnt++;
}
}
}
ans = 0;
for (i = 1; i <= n; i++)
ans = ((long long) ans * X + dp[i] % MD) % MD;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
404 KB |
Output is correct |
15 |
Correct |
17 ms |
8592 KB |
Output is correct |
16 |
Correct |
16 ms |
8584 KB |
Output is correct |
17 |
Correct |
16 ms |
8660 KB |
Output is correct |
18 |
Correct |
20 ms |
8608 KB |
Output is correct |
19 |
Correct |
20 ms |
8528 KB |
Output is correct |
20 |
Correct |
21 ms |
8584 KB |
Output is correct |
21 |
Correct |
17 ms |
12260 KB |
Output is correct |
22 |
Correct |
17 ms |
12316 KB |
Output is correct |
23 |
Correct |
16 ms |
12296 KB |
Output is correct |
24 |
Correct |
16 ms |
12276 KB |
Output is correct |
25 |
Correct |
21 ms |
12328 KB |
Output is correct |
26 |
Correct |
17 ms |
12284 KB |
Output is correct |
27 |
Correct |
17 ms |
12380 KB |
Output is correct |
28 |
Correct |
22 ms |
12364 KB |
Output is correct |
29 |
Correct |
26 ms |
12340 KB |
Output is correct |
30 |
Correct |
26 ms |
12272 KB |
Output is correct |
31 |
Correct |
17 ms |
12252 KB |
Output is correct |
32 |
Correct |
18 ms |
12268 KB |
Output is correct |
33 |
Correct |
18 ms |
12244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
8592 KB |
Output is correct |
2 |
Correct |
16 ms |
8584 KB |
Output is correct |
3 |
Correct |
16 ms |
8660 KB |
Output is correct |
4 |
Correct |
20 ms |
8608 KB |
Output is correct |
5 |
Correct |
20 ms |
8528 KB |
Output is correct |
6 |
Correct |
21 ms |
8584 KB |
Output is correct |
7 |
Correct |
17 ms |
12260 KB |
Output is correct |
8 |
Correct |
17 ms |
12316 KB |
Output is correct |
9 |
Correct |
16 ms |
12296 KB |
Output is correct |
10 |
Correct |
16 ms |
12276 KB |
Output is correct |
11 |
Correct |
21 ms |
12328 KB |
Output is correct |
12 |
Correct |
17 ms |
12284 KB |
Output is correct |
13 |
Correct |
17 ms |
12380 KB |
Output is correct |
14 |
Correct |
22 ms |
12364 KB |
Output is correct |
15 |
Correct |
26 ms |
12340 KB |
Output is correct |
16 |
Correct |
26 ms |
12272 KB |
Output is correct |
17 |
Correct |
17 ms |
12252 KB |
Output is correct |
18 |
Correct |
18 ms |
12268 KB |
Output is correct |
19 |
Correct |
18 ms |
12244 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
316 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
404 KB |
Output is correct |
34 |
Correct |
112 ms |
72808 KB |
Output is correct |
35 |
Correct |
117 ms |
72848 KB |
Output is correct |
36 |
Correct |
123 ms |
72896 KB |
Output is correct |
37 |
Correct |
126 ms |
72836 KB |
Output is correct |
38 |
Correct |
114 ms |
72940 KB |
Output is correct |
39 |
Correct |
101 ms |
72964 KB |
Output is correct |
40 |
Correct |
103 ms |
72820 KB |
Output is correct |
41 |
Correct |
108 ms |
72836 KB |
Output is correct |
42 |
Correct |
103 ms |
72924 KB |
Output is correct |
43 |
Correct |
45 ms |
29456 KB |
Output is correct |
44 |
Correct |
78 ms |
45092 KB |
Output is correct |
45 |
Correct |
83 ms |
45132 KB |
Output is correct |
46 |
Correct |
73 ms |
45120 KB |
Output is correct |
47 |
Correct |
113 ms |
74904 KB |
Output is correct |
48 |
Correct |
122 ms |
74892 KB |
Output is correct |
49 |
Correct |
127 ms |
74904 KB |
Output is correct |
50 |
Correct |
112 ms |
74940 KB |
Output is correct |
51 |
Correct |
104 ms |
75040 KB |
Output is correct |
52 |
Correct |
108 ms |
75344 KB |
Output is correct |
53 |
Correct |
101 ms |
75212 KB |
Output is correct |
54 |
Correct |
101 ms |
72876 KB |
Output is correct |
55 |
Correct |
106 ms |
73012 KB |
Output is correct |
56 |
Correct |
104 ms |
72992 KB |
Output is correct |
57 |
Correct |
104 ms |
72920 KB |
Output is correct |
58 |
Correct |
103 ms |
72908 KB |
Output is correct |
59 |
Correct |
104 ms |
72868 KB |
Output is correct |
60 |
Correct |
104 ms |
72892 KB |
Output is correct |
61 |
Correct |
109 ms |
75148 KB |
Output is correct |
62 |
Correct |
103 ms |
74964 KB |
Output is correct |
63 |
Correct |
105 ms |
74848 KB |
Output is correct |