#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[100001];
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});
/* for(int kk=pre[i]+1;kk<i;kk++){
if(it[kk]>it[i]){
while(true){
continue;
}
}
}*/
// 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);
vector<pair<pair<int,int>,int>> cur;
int mo=1e9;
for(int j=i;j<n;j++){
mo=min(mo,dp[j-1]);
if(pre[j]>=i){
dp2[j]=dp2[pre[j]];
}
cur.pb({{j-1,j-1},dp[j-1]});
// cout<<j<<endl;
if(pre[j]==-1){
dp2[j]=min(dp2[j],mo+it[j]);
}
else{
pair<pair<int,int>,int> mi={{-1,-1},1e9};
int mk=1e9;
while(cur.size()){
if(cur.back().a.a>=pre[j]){
if(mi.a.a==-1){
mi=cur.back();
}
else{
mi.a.a=min(mi.a.a,cur.back().a.a);
mi.a.b=max(mi.a.b,cur.back().a.b);
mi.b=min(mi.b,cur.back().b);
}
mk=mi.b;
cur.pop_back();
// cout<<mi.a.a<<","<<mi.a.b<<","<<mi.b<<endl;
}
else{
for(int kk=pre[j];kk<mi.a.a;kk++){
mk=min(mk,dp[kk]);
}
break;
}
}
cur.pb(mi);
dp2[j]=min(dp2[j],mk+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 |
384 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 |
4 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 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 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 |
4 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 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
304 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 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 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
896 KB |
Output is correct |
2 |
Correct |
11 ms |
1024 KB |
Output is correct |
3 |
Correct |
19 ms |
2824 KB |
Output is correct |
4 |
Correct |
108 ms |
2816 KB |
Output is correct |
5 |
Correct |
311 ms |
5572 KB |
Output is correct |
6 |
Correct |
25 ms |
6108 KB |
Output is correct |
7 |
Correct |
165 ms |
6152 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
33 ms |
760 KB |
Output is correct |
15 |
Correct |
6 ms |
768 KB |
Output is correct |
16 |
Correct |
12 ms |
768 KB |
Output is correct |
17 |
Correct |
11 ms |
1408 KB |
Output is correct |
18 |
Correct |
19 ms |
1920 KB |
Output is correct |
19 |
Correct |
91 ms |
1920 KB |
Output is correct |
20 |
Correct |
268 ms |
3584 KB |
Output is correct |
21 |
Correct |
27 ms |
3584 KB |
Output is correct |
22 |
Correct |
138 ms |
3704 KB |
Output is correct |
23 |
Correct |
19 ms |
2560 KB |
Output is correct |
24 |
Correct |
44 ms |
3584 KB |
Output is correct |
25 |
Correct |
271 ms |
3584 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
27 ms |
896 KB |
Output is correct |
29 |
Correct |
7 ms |
896 KB |
Output is correct |
30 |
Correct |
11 ms |
896 KB |
Output is correct |
31 |
Correct |
11 ms |
1664 KB |
Output is correct |
32 |
Correct |
17 ms |
2476 KB |
Output is correct |
33 |
Correct |
89 ms |
2464 KB |
Output is correct |
34 |
Correct |
276 ms |
5440 KB |
Output is correct |
35 |
Correct |
24 ms |
5116 KB |
Output is correct |
36 |
Correct |
128 ms |
5172 KB |
Output is correct |
37 |
Correct |
7 ms |
896 KB |
Output is correct |
38 |
Correct |
27 ms |
960 KB |
Output is correct |
39 |
Correct |
6 ms |
512 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
42 |
Correct |
5 ms |
384 KB |
Output is correct |