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;
typedef pair<int,int> ii;
const int INF = 1e9;
const int MAXN = 1e5 + 10;
const int MAXK = 101;
int vetor[MAXN],dp[MAXK][MAXN],N,K,seg[4*MAXN];
void build(int pos,int left,int right){
if(left == right){
seg[pos] = vetor[left];
return;
}
int mid = (left+right)/2;
build(2*pos,left,mid);
build(2*pos+1,mid+1,right);
seg[pos] = max(seg[2*pos],seg[2*pos+1]);
}
int query(int pos,int left,int right,int i,int j){
if(left >= i && right <= j) return seg[pos];
int mid = (left+right)/2;
if(j <= mid) return query(2*pos,left,mid,i,j);
else if(i >= mid + 1) return query(2*pos+1,mid+1,right,i,j);
return max(query(2*pos,left,mid,i,j),query(2*pos+1,mid+1,right,i,j));
}
void solve(int G,int l1,int l2,int opt1,int opt2){
if(l1 > l2) return;
int lm = (l1+l2)/2;
int best = INF;
int optimal = -1;
for(int cand = opt1;cand<=min(opt2,lm - 1);cand++){
int novo = dp[G-1][cand] + query(1,1,N,cand+1,lm);
if(novo < best){
best = novo;
optimal = cand;
}
}
dp[G][lm] = best;
solve(G,l1,lm-1,opt1,optimal);
solve(G,lm+1,l2,optimal,opt2);
}
int main(){
scanf("%d %d",&N,&K);
for(int i = 1;i<=N;i++) scanf("%d",&vetor[i]);
build(1,1,N);
for(int i = 1;i<=N;i++){
dp[1][i] = query(1,1,N,1,i);
}
for(int k = 2;k<=K;k++){
solve(k,k,N,k-1,N-1);
}
printf("%d\n",dp[K][N]);
return 0;
}
Compilation message (stderr)
blocks.cpp: In function 'int main()':
blocks.cpp:42:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&N,&K);
^
blocks.cpp:43:47: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i = 1;i<=N;i++) scanf("%d",&vetor[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... |