This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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]);
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |