#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int K = 105;
const int INF = 1e9;
template <typename T>
class segmentTree {
int n;
vector<T> a;
vector<T> st;
T (*merge)(T, T);
void build(int stI, int L, int R) {
if (L == R) {
st[stI] = a[L];
return;
}
int mid = (L + R) / 2;
build((stI << 1), L, mid);
build((stI << 1) + 1, mid + 1, R);
st[stI] = merge(st[(stI << 1)], st[(stI << 1) + 1]);
}
void update(int stI, int L, int R, int at, T val) {
if (L == R) {
st[stI] = val;
return;
}
int mid = (L + R) / 2;
if (at <= mid) update((stI << 1), L, mid, at, val);
else update((stI << 1) + 1, mid + 1, R, at, val);
st[stI] = merge(st[(stI << 1)], st[(stI << 1) + 1]);
}
T query(int stI, int L, int R, int l, int r) {
if (l <= L && R <= r) return st[stI];
int mid = (L + R) / 2;
if (r <= mid) return query((stI << 1), L, mid, l, r);
if (mid + 1 <= l) return query((stI << 1) + 1, mid + 1, R, l, r);
return merge(
query((stI << 1), L, mid, l, mid),
query((stI << 1) + 1, mid + 1, R, mid + 1, r)
);
}
public:
segmentTree(vector<T> _a, T (*_merge)(T, T)) {
n = _a.size();
a = _a;
st.resize(4 * n + 1);
merge = _merge;
build(1, 0, n - 1);
}
T query(int l, int r) { // range [l, r], 0-based index
return query(1, 0, n - 1, l, r);
}
void update(int at, T val) { // at is 0-based index
update(1, 0, n - 1, at, val);
}
};
int n, k;
int a[N];
int l[N];
int dp[K][N];
segmentTree<int>* st;
int main() {
#ifdef LOCAL
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
cin >> n >> k;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) {
int at = i - 1;
while (at > 0 && a[at] < a[i]) at = l[at];
l[i] = at;
}
fill(&dp[0][0], &dp[K][0], INF);
dp[0][0] = 0;
st = new segmentTree<int>(vector<int>(&dp[0][0], &dp[0][n + 1]), [](int i, int j) {return min(i, j);});
for (int _k = 1; _k <= k; _k++) {
for (int i = _k; i <= n; i++) {
dp[_k][i] = min(a[i] + st->query(l[i], i - 1), dp[_k][l[i]]);
}
st = new segmentTree<int>(vector<int>(&dp[_k][0], &dp[_k][n + 1]), [](int i, int j) {return min(i, j);});
}
cout << dp[k][n] << endl;
return 0;
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:80:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
41464 KB |
Output is correct |
2 |
Correct |
39 ms |
41464 KB |
Output is correct |
3 |
Correct |
39 ms |
41464 KB |
Output is correct |
4 |
Correct |
40 ms |
41456 KB |
Output is correct |
5 |
Correct |
39 ms |
41464 KB |
Output is correct |
6 |
Correct |
43 ms |
41464 KB |
Output is correct |
7 |
Correct |
43 ms |
41464 KB |
Output is correct |
8 |
Correct |
42 ms |
41464 KB |
Output is correct |
9 |
Correct |
41 ms |
41420 KB |
Output is correct |
10 |
Correct |
42 ms |
41464 KB |
Output is correct |
11 |
Correct |
39 ms |
41464 KB |
Output is correct |
12 |
Correct |
40 ms |
41464 KB |
Output is correct |
13 |
Correct |
69 ms |
41500 KB |
Output is correct |
14 |
Correct |
43 ms |
41464 KB |
Output is correct |
15 |
Correct |
41 ms |
41480 KB |
Output is correct |
16 |
Correct |
44 ms |
41464 KB |
Output is correct |
17 |
Correct |
39 ms |
41464 KB |
Output is correct |
18 |
Correct |
40 ms |
41464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
41464 KB |
Output is correct |
2 |
Correct |
40 ms |
41464 KB |
Output is correct |
3 |
Correct |
40 ms |
41464 KB |
Output is correct |
4 |
Correct |
42 ms |
41464 KB |
Output is correct |
5 |
Correct |
44 ms |
41412 KB |
Output is correct |
6 |
Correct |
41 ms |
41464 KB |
Output is correct |
7 |
Correct |
39 ms |
41464 KB |
Output is correct |
8 |
Correct |
43 ms |
41464 KB |
Output is correct |
9 |
Correct |
42 ms |
41464 KB |
Output is correct |
10 |
Correct |
42 ms |
41464 KB |
Output is correct |
11 |
Correct |
39 ms |
41464 KB |
Output is correct |
12 |
Correct |
39 ms |
41468 KB |
Output is correct |
13 |
Correct |
39 ms |
41456 KB |
Output is correct |
14 |
Correct |
40 ms |
41464 KB |
Output is correct |
15 |
Correct |
40 ms |
41436 KB |
Output is correct |
16 |
Correct |
39 ms |
41464 KB |
Output is correct |
17 |
Correct |
39 ms |
41464 KB |
Output is correct |
18 |
Correct |
39 ms |
41472 KB |
Output is correct |
19 |
Correct |
39 ms |
41464 KB |
Output is correct |
20 |
Correct |
39 ms |
41464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
41464 KB |
Output is correct |
2 |
Correct |
40 ms |
41444 KB |
Output is correct |
3 |
Correct |
39 ms |
41464 KB |
Output is correct |
4 |
Correct |
39 ms |
41592 KB |
Output is correct |
5 |
Correct |
39 ms |
41464 KB |
Output is correct |
6 |
Correct |
39 ms |
41464 KB |
Output is correct |
7 |
Correct |
39 ms |
41432 KB |
Output is correct |
8 |
Correct |
39 ms |
41464 KB |
Output is correct |
9 |
Correct |
39 ms |
41340 KB |
Output is correct |
10 |
Correct |
40 ms |
41464 KB |
Output is correct |
11 |
Correct |
43 ms |
41464 KB |
Output is correct |
12 |
Correct |
43 ms |
41464 KB |
Output is correct |
13 |
Correct |
39 ms |
41464 KB |
Output is correct |
14 |
Correct |
40 ms |
41464 KB |
Output is correct |
15 |
Correct |
40 ms |
41592 KB |
Output is correct |
16 |
Correct |
41 ms |
41720 KB |
Output is correct |
17 |
Correct |
39 ms |
41592 KB |
Output is correct |
18 |
Correct |
40 ms |
41592 KB |
Output is correct |
19 |
Correct |
40 ms |
41592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
46840 KB |
Output is correct |
2 |
Correct |
53 ms |
44048 KB |
Output is correct |
3 |
Correct |
83 ms |
49232 KB |
Output is correct |
4 |
Correct |
443 ms |
109512 KB |
Output is correct |
5 |
Execution timed out |
1095 ms |
209776 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |