This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define X first
#define Y second
#define SZ(x) (int)(x).size()
int n;int K;
int ps[100005];
int dp[100005][2];
signed frm[100005][205];
deque<signed>dq;
int f(int x,int id,int k){
return ps[id]*x+dp[id][k]-ps[id]*ps[id];
}
bool chk(int id1,int id2,int id3,int k){
__int128 a1;__int128 a2;__int128 a3;__int128 b1;__int128 b2;__int128 b3;
a1=ps[id1];a2=ps[id2];a3=ps[id3];
b1=dp[id1][k]-ps[id1]*ps[id1];
b2=dp[id2][k]-ps[id2]*ps[id2];
b3=dp[id3][k]-ps[id3]*ps[id3];
if(a2==a3){return 1;}
//return (b2-b1)/(a1-a2)>=(b3-b1)/(a1-a3);
return (b2-b1)*(a1-a3)>=(b3-b1)*(a1-a2);
}
signed main(){
cin>>n>>K;
K+=1;
for(int i=1;i<=n;i++){
cin>>ps[i];
ps[i]+=ps[i-1];
}
dp[1][1]=0;
for(int j=2;j<=K;j++){
while(dq.size()){dq.pop_back();}
dq.push_back(0);
int tmp=j;
j=1;
for(int i=1;i<=n;i++){
while(dq.size()>=2&&f(ps[i],dq.front(),j-1)<=f(ps[i],dq[1],j-1)){dq.pop_front();}
dp[i][j]=f(ps[i],dq.front(),j-1);
frm[i][tmp]=dq.front();
while(dq.size()>=2&&chk(dq[0],dq[1],i,j-1)){dq.pop_back();}
if(dq.size()==1&&ps[i]==ps[dq.front()]){dq.pop_front();}
dq.push_back(i);
/*
for(int j=K;j>=2;j--){
while(dq[j-1].size()>=2&&f(ps[i],dq[j-1].front(),j-1)<=f(ps[i],dq[j-1][1],j-1)){dq[j-1].pop_front();}
dp[i][j]=f(ps[i],dq[j-1].front(),j-1);
frm[i][j]=dq[j-1].front();
while(dq[j].size()>=2&&chk(dq[j][0],dq[j][1],i,j)){dq[j].pop_back();}
if(dq[j].size()==1&&ps[i]==ps[dq[j].front()]){dq[j].pop_front();}
dq[j].push_back(i);
}
int j=1;
while(dq[j].size()>=2&&chk(dq[j][0],dq[j][1],i,j)){dq[j].pop_back();}
if(dq[j].size()==1&&ps[i]==ps[dq[j].front()]){dq[j].pop_front();}
dq[j].push_back(i);
*/
}
for(int i=0;i<=n;i++){
swap(dp[i][0],dp[i][1]);
dp[i][1]=0;
}
j=tmp;
}
/*
for(int i=1;i<=n;i++){
for(int j=1;j<=K;j++){
cout<<dp[i][j]<<" ";
}cout<<"\n";
}*/
cout<<dp[n][0]<<"\n";
int nw=n;
int k=K;
vector<int>vc;
for(int i=1;i<=K-1;i++){
nw=frm[nw][k];
vc.push_back(nw);
k--;
}
reverse(vc.begin(),vc.end());
for(int i=0;i<vc.size();i++){
cout<<vc[i]<<" ";
}
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:96:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for(int i=0;i<vc.size();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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |