#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define sz(a) (int)(a.size())
#define all(a) a.begin(), a.end()
#define uni(a) sort(all(a)), a.resize(unique(all(a)) - a.begin())
typedef pair <int, int> ii;
const int N = 1e5 + 5;
int n, k, a[N], ans, l[N], r[N], dp[101][N];
stack <int> st;
int mn[N][20];
int *save;
void build() {
for (int i = 0; i <= n; i++) mn[i][0] = save[i];
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 0; i + (1 << j) - 1 <= n; i++) {
mn[i][j] = min(mn[i][j - 1], mn[i + (1 << j - 1)][j - 1]);
}
}
}
int get(int l, int r) {
if (l > r) return 1e9;
int lg = __lg(r - l + 1);
return min(mn[l][lg], mn[r - (1 << lg) + 1][lg]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen("a.inp", "r", stdin);
//freopen("a.out", "w", stdout);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
a[0] = a[n + 1] = 1e9;
st.push(0);
for (int i = 1; i <= n; i++) {
while (a[st.top()] <= a[i]) st.pop();
l[i] = st.top();
st.push(i);
}
while (sz(st)) st.pop();
st.push(n + 1);
for (int i = n; i >= 1; i--) {
while (a[st.top()] <= a[i]) st.pop();
r[i] = st.top();
st.push(i);
}
memset(dp, 0x3f, sizeof(dp));
dp[0][0] = 0; save = dp[0];
for (int j = 1; j <= k; j++) {
build();
for (int i = 1; i <= n; i++) {
dp[j][i] = get(l[i], i - 1) + a[i];
dp[j][i] = min(dp[j][i], dp[j][l[i]]);
}
save = dp[j];
}
cout << dp[k][n] << '\n';
return 0;
}
Compilation message
blocks.cpp: In function 'void build()':
blocks.cpp:25:57: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
25 | mn[i][j] = min(mn[i][j - 1], mn[i + (1 << j - 1)][j - 1]);
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
41296 KB |
Output is correct |
2 |
Correct |
6 ms |
41296 KB |
Output is correct |
3 |
Correct |
5 ms |
41296 KB |
Output is correct |
4 |
Correct |
6 ms |
41296 KB |
Output is correct |
5 |
Correct |
7 ms |
41396 KB |
Output is correct |
6 |
Correct |
7 ms |
41296 KB |
Output is correct |
7 |
Correct |
7 ms |
41296 KB |
Output is correct |
8 |
Correct |
5 ms |
41296 KB |
Output is correct |
9 |
Correct |
6 ms |
41296 KB |
Output is correct |
10 |
Correct |
6 ms |
41492 KB |
Output is correct |
11 |
Correct |
6 ms |
41460 KB |
Output is correct |
12 |
Correct |
5 ms |
41468 KB |
Output is correct |
13 |
Correct |
7 ms |
41296 KB |
Output is correct |
14 |
Correct |
6 ms |
41296 KB |
Output is correct |
15 |
Correct |
5 ms |
41512 KB |
Output is correct |
16 |
Correct |
5 ms |
41296 KB |
Output is correct |
17 |
Correct |
6 ms |
41296 KB |
Output is correct |
18 |
Correct |
7 ms |
41296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
41476 KB |
Output is correct |
2 |
Correct |
6 ms |
41296 KB |
Output is correct |
3 |
Correct |
6 ms |
41456 KB |
Output is correct |
4 |
Correct |
7 ms |
41296 KB |
Output is correct |
5 |
Correct |
6 ms |
41296 KB |
Output is correct |
6 |
Correct |
5 ms |
41296 KB |
Output is correct |
7 |
Correct |
6 ms |
41308 KB |
Output is correct |
8 |
Correct |
6 ms |
41296 KB |
Output is correct |
9 |
Correct |
6 ms |
41296 KB |
Output is correct |
10 |
Correct |
5 ms |
41296 KB |
Output is correct |
11 |
Correct |
6 ms |
41296 KB |
Output is correct |
12 |
Correct |
6 ms |
41464 KB |
Output is correct |
13 |
Correct |
5 ms |
41296 KB |
Output is correct |
14 |
Correct |
7 ms |
41296 KB |
Output is correct |
15 |
Correct |
6 ms |
41296 KB |
Output is correct |
16 |
Correct |
7 ms |
41296 KB |
Output is correct |
17 |
Correct |
6 ms |
41464 KB |
Output is correct |
18 |
Correct |
6 ms |
41296 KB |
Output is correct |
19 |
Correct |
6 ms |
41464 KB |
Output is correct |
20 |
Correct |
5 ms |
41296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
41296 KB |
Output is correct |
2 |
Correct |
6 ms |
41296 KB |
Output is correct |
3 |
Correct |
5 ms |
41296 KB |
Output is correct |
4 |
Correct |
6 ms |
41296 KB |
Output is correct |
5 |
Correct |
7 ms |
41396 KB |
Output is correct |
6 |
Correct |
7 ms |
41296 KB |
Output is correct |
7 |
Correct |
7 ms |
41296 KB |
Output is correct |
8 |
Correct |
5 ms |
41296 KB |
Output is correct |
9 |
Correct |
6 ms |
41296 KB |
Output is correct |
10 |
Correct |
6 ms |
41492 KB |
Output is correct |
11 |
Correct |
6 ms |
41460 KB |
Output is correct |
12 |
Correct |
5 ms |
41468 KB |
Output is correct |
13 |
Correct |
7 ms |
41296 KB |
Output is correct |
14 |
Correct |
6 ms |
41296 KB |
Output is correct |
15 |
Correct |
5 ms |
41512 KB |
Output is correct |
16 |
Correct |
5 ms |
41296 KB |
Output is correct |
17 |
Correct |
6 ms |
41296 KB |
Output is correct |
18 |
Correct |
7 ms |
41296 KB |
Output is correct |
19 |
Correct |
7 ms |
41476 KB |
Output is correct |
20 |
Correct |
6 ms |
41296 KB |
Output is correct |
21 |
Correct |
6 ms |
41456 KB |
Output is correct |
22 |
Correct |
7 ms |
41296 KB |
Output is correct |
23 |
Correct |
6 ms |
41296 KB |
Output is correct |
24 |
Correct |
5 ms |
41296 KB |
Output is correct |
25 |
Correct |
6 ms |
41308 KB |
Output is correct |
26 |
Correct |
6 ms |
41296 KB |
Output is correct |
27 |
Correct |
6 ms |
41296 KB |
Output is correct |
28 |
Correct |
5 ms |
41296 KB |
Output is correct |
29 |
Correct |
6 ms |
41296 KB |
Output is correct |
30 |
Correct |
6 ms |
41464 KB |
Output is correct |
31 |
Correct |
5 ms |
41296 KB |
Output is correct |
32 |
Correct |
7 ms |
41296 KB |
Output is correct |
33 |
Correct |
6 ms |
41296 KB |
Output is correct |
34 |
Correct |
7 ms |
41296 KB |
Output is correct |
35 |
Correct |
6 ms |
41464 KB |
Output is correct |
36 |
Correct |
6 ms |
41296 KB |
Output is correct |
37 |
Correct |
6 ms |
41464 KB |
Output is correct |
38 |
Correct |
5 ms |
41296 KB |
Output is correct |
39 |
Correct |
5 ms |
41296 KB |
Output is correct |
40 |
Correct |
6 ms |
41296 KB |
Output is correct |
41 |
Correct |
6 ms |
41464 KB |
Output is correct |
42 |
Correct |
6 ms |
41296 KB |
Output is correct |
43 |
Correct |
6 ms |
41296 KB |
Output is correct |
44 |
Correct |
6 ms |
41296 KB |
Output is correct |
45 |
Correct |
6 ms |
41296 KB |
Output is correct |
46 |
Correct |
5 ms |
41296 KB |
Output is correct |
47 |
Correct |
6 ms |
41296 KB |
Output is correct |
48 |
Correct |
6 ms |
41296 KB |
Output is correct |
49 |
Correct |
6 ms |
41296 KB |
Output is correct |
50 |
Correct |
5 ms |
41444 KB |
Output is correct |
51 |
Correct |
5 ms |
41296 KB |
Output is correct |
52 |
Correct |
5 ms |
41484 KB |
Output is correct |
53 |
Correct |
7 ms |
41552 KB |
Output is correct |
54 |
Correct |
6 ms |
41296 KB |
Output is correct |
55 |
Correct |
6 ms |
41296 KB |
Output is correct |
56 |
Correct |
6 ms |
41296 KB |
Output is correct |
57 |
Correct |
6 ms |
41296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
41296 KB |
Output is correct |
2 |
Correct |
6 ms |
41296 KB |
Output is correct |
3 |
Correct |
5 ms |
41296 KB |
Output is correct |
4 |
Correct |
6 ms |
41296 KB |
Output is correct |
5 |
Correct |
7 ms |
41396 KB |
Output is correct |
6 |
Correct |
7 ms |
41296 KB |
Output is correct |
7 |
Correct |
7 ms |
41296 KB |
Output is correct |
8 |
Correct |
5 ms |
41296 KB |
Output is correct |
9 |
Correct |
6 ms |
41296 KB |
Output is correct |
10 |
Correct |
6 ms |
41492 KB |
Output is correct |
11 |
Correct |
6 ms |
41460 KB |
Output is correct |
12 |
Correct |
5 ms |
41468 KB |
Output is correct |
13 |
Correct |
7 ms |
41296 KB |
Output is correct |
14 |
Correct |
6 ms |
41296 KB |
Output is correct |
15 |
Correct |
5 ms |
41512 KB |
Output is correct |
16 |
Correct |
5 ms |
41296 KB |
Output is correct |
17 |
Correct |
6 ms |
41296 KB |
Output is correct |
18 |
Correct |
7 ms |
41296 KB |
Output is correct |
19 |
Correct |
7 ms |
41476 KB |
Output is correct |
20 |
Correct |
6 ms |
41296 KB |
Output is correct |
21 |
Correct |
6 ms |
41456 KB |
Output is correct |
22 |
Correct |
7 ms |
41296 KB |
Output is correct |
23 |
Correct |
6 ms |
41296 KB |
Output is correct |
24 |
Correct |
5 ms |
41296 KB |
Output is correct |
25 |
Correct |
6 ms |
41308 KB |
Output is correct |
26 |
Correct |
6 ms |
41296 KB |
Output is correct |
27 |
Correct |
6 ms |
41296 KB |
Output is correct |
28 |
Correct |
5 ms |
41296 KB |
Output is correct |
29 |
Correct |
6 ms |
41296 KB |
Output is correct |
30 |
Correct |
6 ms |
41464 KB |
Output is correct |
31 |
Correct |
5 ms |
41296 KB |
Output is correct |
32 |
Correct |
7 ms |
41296 KB |
Output is correct |
33 |
Correct |
6 ms |
41296 KB |
Output is correct |
34 |
Correct |
7 ms |
41296 KB |
Output is correct |
35 |
Correct |
6 ms |
41464 KB |
Output is correct |
36 |
Correct |
6 ms |
41296 KB |
Output is correct |
37 |
Correct |
6 ms |
41464 KB |
Output is correct |
38 |
Correct |
5 ms |
41296 KB |
Output is correct |
39 |
Correct |
5 ms |
41296 KB |
Output is correct |
40 |
Correct |
6 ms |
41296 KB |
Output is correct |
41 |
Correct |
6 ms |
41464 KB |
Output is correct |
42 |
Correct |
6 ms |
41296 KB |
Output is correct |
43 |
Correct |
6 ms |
41296 KB |
Output is correct |
44 |
Correct |
6 ms |
41296 KB |
Output is correct |
45 |
Correct |
6 ms |
41296 KB |
Output is correct |
46 |
Correct |
5 ms |
41296 KB |
Output is correct |
47 |
Correct |
6 ms |
41296 KB |
Output is correct |
48 |
Correct |
6 ms |
41296 KB |
Output is correct |
49 |
Correct |
6 ms |
41296 KB |
Output is correct |
50 |
Correct |
5 ms |
41444 KB |
Output is correct |
51 |
Correct |
5 ms |
41296 KB |
Output is correct |
52 |
Correct |
5 ms |
41484 KB |
Output is correct |
53 |
Correct |
7 ms |
41552 KB |
Output is correct |
54 |
Correct |
6 ms |
41296 KB |
Output is correct |
55 |
Correct |
6 ms |
41296 KB |
Output is correct |
56 |
Correct |
6 ms |
41296 KB |
Output is correct |
57 |
Correct |
6 ms |
41296 KB |
Output is correct |
58 |
Correct |
13 ms |
43856 KB |
Output is correct |
59 |
Correct |
11 ms |
46160 KB |
Output is correct |
60 |
Correct |
22 ms |
46160 KB |
Output is correct |
61 |
Correct |
87 ms |
46120 KB |
Output is correct |
62 |
Correct |
333 ms |
48976 KB |
Output is correct |
63 |
Correct |
24 ms |
49748 KB |
Output is correct |
64 |
Correct |
182 ms |
49740 KB |
Output is correct |
65 |
Correct |
6 ms |
41300 KB |
Output is correct |
66 |
Correct |
5 ms |
41300 KB |
Output is correct |
67 |
Correct |
6 ms |
41300 KB |
Output is correct |
68 |
Correct |
6 ms |
41300 KB |
Output is correct |
69 |
Correct |
6 ms |
41720 KB |
Output is correct |
70 |
Correct |
6 ms |
41556 KB |
Output is correct |
71 |
Correct |
27 ms |
43860 KB |
Output is correct |
72 |
Correct |
7 ms |
43860 KB |
Output is correct |
73 |
Correct |
13 ms |
43860 KB |
Output is correct |
74 |
Correct |
12 ms |
46420 KB |
Output is correct |
75 |
Correct |
19 ms |
46364 KB |
Output is correct |
76 |
Correct |
103 ms |
46612 KB |
Output is correct |
77 |
Correct |
339 ms |
49664 KB |
Output is correct |
78 |
Correct |
30 ms |
49492 KB |
Output is correct |
79 |
Correct |
126 ms |
49492 KB |
Output is correct |
80 |
Correct |
20 ms |
49504 KB |
Output is correct |
81 |
Correct |
46 ms |
49492 KB |
Output is correct |
82 |
Correct |
280 ms |
49500 KB |
Output is correct |
83 |
Correct |
6 ms |
41556 KB |
Output is correct |
84 |
Correct |
7 ms |
41556 KB |
Output is correct |
85 |
Correct |
27 ms |
43860 KB |
Output is correct |
86 |
Correct |
7 ms |
43860 KB |
Output is correct |
87 |
Correct |
13 ms |
43860 KB |
Output is correct |
88 |
Correct |
11 ms |
46420 KB |
Output is correct |
89 |
Correct |
20 ms |
46420 KB |
Output is correct |
90 |
Correct |
120 ms |
46420 KB |
Output is correct |
91 |
Correct |
266 ms |
49916 KB |
Output is correct |
92 |
Correct |
24 ms |
49748 KB |
Output is correct |
93 |
Correct |
148 ms |
49748 KB |
Output is correct |
94 |
Correct |
8 ms |
43860 KB |
Output is correct |
95 |
Correct |
32 ms |
43860 KB |
Output is correct |
96 |
Correct |
7 ms |
41556 KB |
Output is correct |
97 |
Correct |
6 ms |
41556 KB |
Output is correct |
98 |
Correct |
6 ms |
41300 KB |
Output is correct |
99 |
Correct |
6 ms |
41308 KB |
Output is correct |