#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
//#define endl '\n'
int n,k;
int dp[100001];
int dp2[100001];
int it[100001];
int tree[400001];
int pre[400001];
void build(int no,int l,int r){
if(l==r){
tree[no]=dp[l];
}
else{
int mid=(l+r)/2;
build(no*2+1,l,mid);
build(no*2+2,mid+1,r);
tree[no]=min(tree[no*2+1],tree[no*2+2]);
}
}
int query(int no,int l,int r,int aa,int bb){
if(r<aa or l>bb){
return 1e9;
}
if(aa<=l and r<=bb){
return tree[no];
}
else{
int mid=(l+r)/2;
return min(query(no*2+1,l,mid,aa,bb),query(no*2+2,mid+1,r,aa,bb));
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>k;
vector<pair<int,int>> ac;
for(int i=0;i<n;i++){
cin>>it[i];
while(ac.size()){
if(ac.back().a<it[i]){
ac.pop_back();
}
else{
break;
}
}
if(ac.size()==0){
pre[i]=-1;
}
else{
pre[i]=ac.back().b;
ac.pb({it[i],i});
}
// cout<<pre[i]<<",";
}
//cout<<endl;
int ma=0;
for(int i=0;i<n;i++){
ma=max(ma,it[i]);
dp[i]=ma;
dp2[i]=1e9;
}
for(int i=1;i<k;i++){
build(0,0,n-1);
for(int j=i;j<n;j++){
if(pre[j]>=i){
dp2[j]=dp2[pre[j]];
}
if(pre[j]==-1){
dp2[j]=min(dp2[j],query(0,0,n-1,0,j-1)+it[j]);
}
else{
dp2[j]=min(dp2[j],query(0,0,n-1,pre[j],j-1)+it[j]);
}
}
for(int j=0;j<n;j++){
dp[j]=dp2[j];
dp2[j]=1e9;
}
}
cout<<dp[n-1]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
396 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
768 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |