Submission #97992

#TimeUsernameProblemLanguageResultExecution timeMemory
97992Bodo171Split the sequence (APIO14_sequence)C++14
0 / 100
24 ms4700 KiB
#include <iostream> #include <fstream> #include <algorithm> using namespace std; const int nmax=100005; struct dreapta { long long a,b; int wh; }st[nmax]; long long dp[2][nmax]; int prc[205][nmax]; long long mx; long long a[nmax],sum[nmax]; long double a1,a2,b1,b2; int ans[nmax]; long double intersect(dreapta unu,dreapta doi) { a1=unu.a;b1=unu.b;a2=doi.a;b2=doi.b; return (b1-b2)/(a2-a1); } int u,i,j,cnt,ult,n,k,use,poz_opt; void ins(long long A,long long B,int prc) { dreapta x={A,B,prc}; if(u&&st[u].a==A&&st[u].b>=B) return; while(u&&st[u].a==A&&st[u].b<=B) u--; while(u>1&&intersect(st[u],st[u-1])>intersect(st[u-1],x)) u--; st[++u]=x; } long long get_opt(long long x) { long double pct=x; while(u>1&&intersect(st[u],st[u-1])>pct) u--; ult=st[u].wh; return (1LL*st[u].a*x+1LL*st[u].b); } void solve(bool rev) { bool updatat=0; for(i=1;i<=n;i++) dp[0][i]=dp[1][i]=0; for(i=1;i<=n;i++) sum[i]=sum[i-1]+a[i]; for(cnt=1;cnt<=k;cnt++) { u=0;use=1-use; for(i=cnt;i<n;i++) { ins(sum[i-1],dp[1-use][i-1],i-1); dp[use][i]=get_opt(sum[i]-sum[n])+1LL*sum[i]*sum[n]-1LL*sum[i]*sum[i]; prc[cnt][i]=ult; if(cnt==k&&dp[use][i]>mx) mx=dp[use][i],poz_opt=i,updatat=1; } } if(updatat) { for(int i=1;i<=k;i++) { ans[k-i+1]=poz_opt; poz_opt=prc[k-i+1][poz_opt]; } if(rev) for(i=1;i<=k;i++) ans[i]=(n-ans[i]+1); } } int main() { // freopen("data.in","r",stdin); ios_base::sync_with_stdio(false); cin>>n>>k; for(i=1;i<=n;i++) cin>>a[i]; solve(0); reverse(a+1,a+n+1); //solve(1); cout<<mx<<'\n'; for(i=1;i<=k;i++) cout<<ans[i]<<' '; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...