Submission #7646

#TimeUsernameProblemLanguageResultExecution timeMemory
7646myungwooSplit the sequence (APIO14_sequence)C++98
0 / 100
0 ms83512 KiB
#include <stdio.h> #define MAXN 100005 #define MAXK 202 typedef long long lld; int N,K,A[MAXN],S[MAXN],P[MAXN][MAXK]; int stk[MAXN],scnt; lld D[MAXN],E[MAXN],F[MAXN]; bool check(int a,int b,int c) { return (F[a]-F[b])*(S[b]-S[c]) <= (F[b]-F[c])*(S[a]-S[b]); } int main() { int i,j,k; scanf("%d%d",&N,&K); ++K; for (i=1;i<=N;i++) scanf("%d",A+i), S[i] = S[i-1]+A[i]; stk[++scnt] = 0; for (j=1;j<=K;j++){ int p = 1; for (i=j;i<=N;i++){ while (p < scnt && stk[p+1] < i && F[stk[p]]+S[stk[p]]*S[i] <= F[stk[p+1]]+S[stk[p+1]]*S[i]) p++; int t = stk[p]; D[i] = E[t]+(lld)(S[i]-S[t])*S[t]; P[i][j] = t; } for (i=j;i<=N;i++) F[i] = D[i]-S[i]*S[i]; scnt = 0; stk[++scnt] = j; for (i=j+1;i<=N;i++) if (S[i]){ while (scnt > 1 && check(stk[scnt-1],stk[scnt],i)) scnt--; stk[++scnt] = i; } for (i=1;i<=N;i++) E[i] = D[i]; } printf("%lld\n",D[N]); for (i=N,j=K;j;){ if (i < N) printf("%d ",i); i = P[i][j--]; } puts(""); }
#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...