Submission #403036

#TimeUsernameProblemLanguageResultExecution timeMemory
403036Waratpp123Split the sequence (APIO14_sequence)C++14
100 / 100
879 ms81764 KiB
#include<bits/stdc++.h> using namespace std; long long a[100010],qs[100010],dp[2][100010]; int p[205][100010],n,k; void play(int st,int l,int r,int optl,int optr){ int mid=(l+r)/2,pos,i; long long mi=1e18; if(l>r) return; for(i=optl;i<=min(mid,optr);i++){ if(dp[1-st%2][i-1]+(qs[mid]-qs[i-1])*(qs[mid]-qs[i-1])<=mi){ mi=dp[1-st%2][i-1]+(qs[mid]-qs[i-1])*(qs[mid]-qs[i-1]); pos=i; } } dp[st%2][mid]=mi; p[st][mid]=pos-1; play(st,l,mid-1,optl,pos); play(st,mid+1,r,pos,optr); } int main(){ int i,j,now; scanf("%d %d",&n,&k); for(i=1;i<=n;i++){ scanf("%lld",&a[i]); qs[i]=qs[i-1]+a[i]; } for(i=1;i<=n;i++){ dp[0][i]=qs[i]*qs[i]; } for(i=1;i<=k;i++){ play(i,1,n,1,n); } printf("%lld\n",(qs[n]*qs[n]-dp[k%2][n])/2); now=n; for(i=k;i>=1;i--){ printf("%d ",p[i][now]); now=p[i][now]; } return 0; } /* 7 3 4 1 3 4 0 2 3 */

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:21:11: warning: unused variable 'j' [-Wunused-variable]
   21 |     int i,j,now;
      |           ^
sequence.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |     scanf("%d %d",&n,&k);
      |     ~~~~~^~~~~~~~~~~~~~~
sequence.cpp:24:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |         scanf("%lld",&a[i]);
      |         ~~~~~^~~~~~~~~~~~~~
sequence.cpp: In function 'void play(int, int, int, int, int)':
sequence.cpp:16:19: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
   16 |     p[st][mid]=pos-1;
      |                ~~~^~
#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...