# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
37919 | Just_Solve_The_Problem | K blocks (IZhO14_blocks) | C++11 | 326 ms | 12172 KiB |
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;
#define ll long long
const int N = (int)1e5 + 7;
const ll linf = (ll)1e15 + 7;
ll dp[2][N];
int ar[N];
int numlog[N];
int table[20][N];
int n;
void buildtable () {
for (int i = 2; i <= n; i++) {
numlog[i] = numlog[i / 2] + 1;
}
for (int i = 0; i <= numlog[n]; i++) {
int curlen = 1 << i;
for (int j = 1; j <= n; j++) {
if (i == 0) {
table[i][j] = ar[j];
continue;
}
table[i][j] = max(table[i - 1][j], table[i - 1][j + curlen / 2]);
}
}
}
ll get (int l, int r) {
if (l > r) return linf;
int curlog = numlog[r - l + 1];
return max(table[curlog][l], table[curlog][r - (1 << curlog) + 1]);
}
void div (int k, int l, int r, int optl, int optr) {
if (l > r) return ;
int mid = (l + r) >> 1;
int opt = optl;
dp[k & 1][mid] = linf;
for (int i = optl; i <= min(mid, optr); i++) {
ll cur = dp[k & 1 ^ 1][i] + get(i + 1, mid);
if (cur < dp[k & 1][mid]) {
dp[k & 1][mid] = cur;
opt = i;
}
}
div(k, l, mid - 1, optl, opt);
div(k, mid + 1, r, opt, optr);
}
main() {
int k;
scanf ("%d %d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf ("%d", ar + i);
}
buildtable();
for (int i = 0; i <= n; i++) {
dp[0][i] = linf;
}
for (int i = 1; i <= n; i++) {
dp[1][i] = get(1, i);
}
// cout << get(1, n) << endl;
// for (int i = 1; i <= n; i++) {
// cout << dp[1][i] << ' ';
// }
// puts("");
for (int i = 2; i <= k; i++) {
div(i, 1, n, 1, n - 1);
// for (int j = 1; j <= n; j++) {
// cout << dp[i & 1][j] << ' ';
// }
// puts("");
}
cout << dp[k & 1][n];
}
Compilation message (stderr)
# | 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... |