제출 #7650

#제출 시각아이디문제언어결과실행 시간메모리
7650myungwoo수열 (APIO14_sequence)C++98
100 / 100
680 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 (lld)(F[a]-F[b])*(S[b]-S[c]) <= (lld)(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]]+(lld)S[stk[p]]*S[i] <= F[stk[p+1]]+(lld)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; //printf("D[%d][%d]: %d, P[%d][%d]: %d\n",i,j,D[i],i,j,t); } for (i=j;i<=N;i++) F[i] = D[i]-(lld)S[i]*S[i]; scnt = 0; for (i=j;i<=N;i++) if (i == j || S[i-1] != 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...