Submission #98011

#TimeUsernameProblemLanguageResultExecution timeMemory
98011Bodo171Split the sequence (APIO14_sequence)C++14
100 / 100
1188 ms84348 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]; int p; 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(p<=u&&st[u].a==A&&st[u].b>=B) return; while(p<=u&&st[u].a==A&&st[u].b<=B) u--; while(p<u&&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(p<u&&intersect(st[p],st[p+1])<pct) p++; ult=st[p].wh; return (1LL*st[p].a*x+1LL*st[p].b); } void solve(bool rev) { bool updatat=0; for(i=1;i<=n;i++) sum[i]=sum[i-1]+a[i]; for(cnt=1;cnt<=k;cnt++) { u=0;p=1;use=1-use; for(i=cnt;i<n;i++) { ins(sum[i-1],dp[1-use][i-1],i-1); dp[use][i]=1LL*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=1LL*dp[use][i],poz_opt=i; } } for(int i=1;i<=k;i++) { ans[k-i+1]=poz_opt; poz_opt=prc[k-i+1][poz_opt]; } } int main() { //freopen("data.in","r",stdin); ios_base::sync_with_stdio(false); mx=-1; cin>>n>>k; for(i=1;i<=n;i++) cin>>a[i]; solve(0); cout<<mx<<'\n'; for(i=1;i<=k;i++) cout<<ans[i]<<' '; return 0; }

Compilation message (stderr)

sequence.cpp: In function 'void solve(bool)':
sequence.cpp:45:10: warning: unused variable 'updatat' [-Wunused-variable]
     bool updatat=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...