#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 7;
const int inf = (int)2e9 + 7;
int n, k;
int dp[2][N];
int a[N];
struct sparse {
int table[19][N];
int numlog[N];
void build() {
for (int i = 2; i < N; i++) {
numlog[i] = numlog[i / 2] + 1;
}
for (int i = 0; i <= 17; i++) {
for (int j = 1; j <= n; j++) {
if (!i) {
table[i][j] = a[j];
} else {
table[i][j] = max(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]);
}
}
}
}
int getmax(int l, int r) {
int lg = numlog[r - l + 1];
return max(table[lg][l], table[lg][r - (1 << lg) + 1]);
}
};
sparse table;
void div(int id, int l, int r, int optl, int optr) {
if (l > r) return ;
int mid = (l + r) >> 1;
int &res = dp[id][mid];
res = inf;
int opt = optl;
for (int i = optl; i <= min(mid, optr); i++) {
int cost = table.getmax(i, mid) + dp[id ^ 1][i - 1];
if (cost < res) {
res = cost;
opt = i;
}
}
//cout << l << ' ' << mid << ' ' << r << ' ' << optl << ' ' << opt << ' ' << optr << ' ' << res << endl;
div(id, l, mid - 1, optl, opt);
div(id, mid + 1, r, opt, optr);
}
main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
table.build();
for (int i = 1; i <= n; i++) {
dp[1][i] = max(dp[1][i - 1], a[i]);
}
for (int i = 2; i <= k; i++) {
div(i & 1, 1, n, i, n);
}
printf("%d\n", dp[k & 1][n]);
}
Compilation message
blocks.cpp:54:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
blocks.cpp: In function 'int main()':
blocks.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~~
blocks.cpp:57:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
760 KB |
Output is correct |
2 |
Correct |
2 ms |
896 KB |
Output is correct |
3 |
Correct |
2 ms |
896 KB |
Output is correct |
4 |
Correct |
2 ms |
996 KB |
Output is correct |
5 |
Correct |
2 ms |
996 KB |
Output is correct |
6 |
Correct |
2 ms |
996 KB |
Output is correct |
7 |
Correct |
2 ms |
1052 KB |
Output is correct |
8 |
Correct |
2 ms |
1052 KB |
Output is correct |
9 |
Correct |
2 ms |
1052 KB |
Output is correct |
10 |
Correct |
2 ms |
1052 KB |
Output is correct |
11 |
Incorrect |
4 ms |
1096 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1132 KB |
Output is correct |
2 |
Correct |
2 ms |
1132 KB |
Output is correct |
3 |
Correct |
2 ms |
1132 KB |
Output is correct |
4 |
Correct |
2 ms |
1132 KB |
Output is correct |
5 |
Correct |
2 ms |
1192 KB |
Output is correct |
6 |
Correct |
2 ms |
1192 KB |
Output is correct |
7 |
Correct |
2 ms |
1192 KB |
Output is correct |
8 |
Correct |
2 ms |
1192 KB |
Output is correct |
9 |
Correct |
3 ms |
1192 KB |
Output is correct |
10 |
Correct |
2 ms |
1192 KB |
Output is correct |
11 |
Incorrect |
2 ms |
1192 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
2000 KB |
Output is correct |
2 |
Correct |
11 ms |
4960 KB |
Output is correct |
3 |
Correct |
24 ms |
5460 KB |
Output is correct |
4 |
Incorrect |
177 ms |
5824 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |