#include <iostream>
using namespace std;
//dp[n][k] = max for n blocks with k partitions
//dp[n][k] = max(dp[n][k],dp[j][k-1]+maxquery(j+1,n))
const int MAXN = 1e5+5;
int arr[MAXN];
pair<int,int> tree2[4*MAXN];
int tree[4*MAXN];
int dp[MAXN][110];
void build(int curr,int l,int r){
if(l==r){
tree[curr] = arr[l];
}else{
int mid = (l+r)/2;
build(2*curr,1,mid);
build(2*curr+1,mid+1,r);
tree[curr] = max(tree[2*curr],tree[2*curr+1]);
}
}
int maxquery(int curr,int l,int r,int tl,int tr){
if(l>tr||r<tl){
return 0;
}else if(l>=tl && r<=tr){
return tree[curr];
}else{
int mid = (l+r)/2;
return max(maxquery(2*curr,1,mid,tl,tr),maxquery(2*curr+1,mid+1,r,tl,tr));
}
}
void build2(int k,int curr,int l,int r){
if(l == r){
tree2[curr] = make_pair(dp[l][k],-1*l);
// cout<<l<<" "<<k<<" "<<dp[l][k]<<endl;
}else{
int mid = (l+r)/2;
build2(k,curr*2,l,mid);
build2(k,2*curr+1,mid+1,r);
tree2[curr] = min(tree2[2*curr],tree2[2*curr+1]);
}
}
pair<int,int> minquery(int curr,int l,int r,int tl,int tr){
//cout<<tr<<endl;
if(l>tr||r<tl){
return make_pair(1e9,1e9);
}else if(l>=tl && r<=tr){
return tree2[curr];
}else{
int mid = (l+r)/2;
return min(minquery(2*curr,1,mid,tl,tr),minquery(2*curr+1,mid+1,r,tl,tr));
}
}
int main(){
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>arr[i];
}
for(int i=1;i<=n;i++){
for(int j=1;j<=k;j++){
dp[i][j] = 1e9;
}
}
build(1,1,n);
dp[1][1] = arr[1];
for(int i=1;i<=k;i++){
build2(i-1,1,1,n);
for(int j=1;j<=n;j++){
auto hold = minquery(1,1,n,1,j-1);
int one = hold.first;
int two = -1*hold.second;
dp[j][i] = min(dp[j][i],one+maxquery(1,1,n,two+1,j));
}
}
cout<<dp[n][k]<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
16 ms |
9216 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |