#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
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]);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
464 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
712 KB |
Output is correct |
6 |
Correct |
1 ms |
712 KB |
Output is correct |
7 |
Correct |
2 ms |
712 KB |
Output is correct |
8 |
Correct |
2 ms |
712 KB |
Output is correct |
9 |
Correct |
2 ms |
712 KB |
Output is correct |
10 |
Correct |
2 ms |
712 KB |
Output is correct |
11 |
Incorrect |
2 ms |
712 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
844 KB |
Output is correct |
2 |
Correct |
2 ms |
844 KB |
Output is correct |
3 |
Correct |
2 ms |
844 KB |
Output is correct |
4 |
Correct |
2 ms |
844 KB |
Output is correct |
5 |
Correct |
2 ms |
844 KB |
Output is correct |
6 |
Correct |
2 ms |
844 KB |
Output is correct |
7 |
Correct |
2 ms |
844 KB |
Output is correct |
8 |
Correct |
2 ms |
896 KB |
Output is correct |
9 |
Correct |
2 ms |
896 KB |
Output is correct |
10 |
Correct |
2 ms |
896 KB |
Output is correct |
11 |
Incorrect |
2 ms |
896 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
1064 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
528 ms |
2304 KB |
Output is correct |
2 |
Correct |
13 ms |
2304 KB |
Output is correct |
3 |
Correct |
770 ms |
3668 KB |
Output is correct |
4 |
Execution timed out |
1070 ms |
4488 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |