#include <bits/stdc++.h>
using namespace std;
// why am I so weak
int n, k;
int a[100055];
int dp[2][100055];
#define MAX_LOG 17
int lg[100055];
int dat[MAX_LOG][100055];
inline int rmq(int x, int y) {
int dist = y - x + 1;
int level = lg[dist];
return max(dat[level][x], dat[level][y - (1 << level) + 1]);
}
inline int f(int i, int j) {
return dp[0][i] + rmq(i + 1, j);
}
void solve(int l, int r, int optl, int optr) {
if (l > r) return;
int mid = (l + r) >> 1;
int opt = optl;
for (int i = optl; i <= min(optr, mid - 1); i++) {
if (f(i, mid) < f(opt, mid)) {
opt = i;
}
}
dp[1][mid] = f(opt, mid);
solve(l, mid - 1, optl, opt);
solve(mid + 1, r, opt, optr);
}
int main() {
// dp[i][j] = min(dp[k][j - 1] + max(k + 1.. i))
// max(k + 1.. i) is non-increasing
// dp[k][j - 1] is non-decreasing
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
dat[0][i] = a[i];
}
for (int i = 2; i <= n; i++) {
lg[i] = lg[i / 2] + 1;
}
for (int i = 0; i + 1 < MAX_LOG; i++) {
for (int j = 1; j + (1 << i) <= n; j++) {
dat[i + 1][j] = max(dat[i][j], dat[i][j + (1 << i)]);
}
}
memset(dp, 63, sizeof(dp));
dp[1][0] = 0;
while (k--) {
swap(dp[0], dp[1]);
memset(dp[1], 63, sizeof(dp[1]));
solve(1, n, 0, n);
}
printf("%d\n", dp[1][n]);
return 0;
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:46:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
^
blocks.cpp:49:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
10228 KB |
Output is correct |
2 |
Correct |
0 ms |
10228 KB |
Output is correct |
3 |
Correct |
0 ms |
10228 KB |
Output is correct |
4 |
Correct |
0 ms |
10228 KB |
Output is correct |
5 |
Correct |
0 ms |
10228 KB |
Output is correct |
6 |
Correct |
0 ms |
10228 KB |
Output is correct |
7 |
Correct |
0 ms |
10228 KB |
Output is correct |
8 |
Correct |
0 ms |
10228 KB |
Output is correct |
9 |
Correct |
0 ms |
10228 KB |
Output is correct |
10 |
Correct |
0 ms |
10228 KB |
Output is correct |
11 |
Incorrect |
0 ms |
10228 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
10228 KB |
Output is correct |
2 |
Correct |
0 ms |
10228 KB |
Output is correct |
3 |
Correct |
0 ms |
10228 KB |
Output is correct |
4 |
Correct |
0 ms |
10228 KB |
Output is correct |
5 |
Correct |
0 ms |
10228 KB |
Output is correct |
6 |
Correct |
0 ms |
10228 KB |
Output is correct |
7 |
Correct |
0 ms |
10228 KB |
Output is correct |
8 |
Correct |
0 ms |
10228 KB |
Output is correct |
9 |
Correct |
0 ms |
10228 KB |
Output is correct |
10 |
Correct |
0 ms |
10228 KB |
Output is correct |
11 |
Incorrect |
0 ms |
10228 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
10228 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
10228 KB |
Output is correct |
2 |
Correct |
13 ms |
10228 KB |
Output is correct |
3 |
Correct |
39 ms |
10228 KB |
Output is correct |
4 |
Incorrect |
323 ms |
10228 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |