# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
21780 | jjwdi0 | K blocks (IZhO14_blocks) | C++11 | 19 ms | 90304 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>
#define log_N 18
using namespace std;
int N, K;
int A[100005];
int D[103][100005], C[103][100005];
int len[100005];
int par[100005][log_N];
void init() {
int k = 0;
for(int i=0; i<=N; i++) {
if(i == (1 << k)) k++;
len[i] = k;
}
}
int RMQ(int s, int e) { return max(par[e][len[e-s]], par[s+(1<<len[e-s])-1][len[e-s]]); }
void D_C(int k, int s, int e, int l, int r) {
if(s > e) return;
int mid = s + e >> 1;
D[k][mid] = 2e9;
for(int i=l; i<=r && i<mid; i++) {
int v = D[k-1][i] + RMQ(i + 1, mid);
if(D[k][mid] > v) D[k][mid] = v, C[k][mid] = i;
}
D_C(k, s, mid - 1, l, C[k][mid]);
D_C(k, mid + 1, e, C[k][mid], r);
}
int main() {
scanf("%d %d", &N, &K);
for(int i=1; i<=N; i++) scanf("%d", A+i);
for(int i=1; i<=N; i++) par[i][0] = A[i];
for(int i=1; i<log_N; i++) for(int j=1; j<=N; j++) par[j][i] = max(par[j][i-1], par[par[j][i-1]][i-1]);
for(int i=1; i<=N; i++) D[1][i] = par[i][log_N-1];
for(int i=2; i<=K; i++) D_C(i, 1, N, 1, N);
printf("%d\n", D[K][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... |