Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 12824 Accepted Submission(s):
3967
#include<iostream> #include<cstdio> #include<cstring> #define N 500005 using namespace std; int dp[N],q[N],sum[N]; int head,tail,n,m; int get_dp(int i,int j) { return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]); } int get_up(int j,int k) { return dp[j]+sum[j]*sum[j]-(dp[k]+sum[k]*sum[k]); } int get_down(int j,int k) { return 2*(sum[j]-sum[k]); } int main() { while(scanf("%d%d",&n,&m)==2) { for(int i=1;i<=n;i++) scanf("%d",&sum[i]); sum[0]=dp[0]=0;head=tail=0; for(int i=1;i<=n;i++) sum[i]+=sum[i-1]; q[tail++]=0; for(int i=1;i<=n;i++) { while(head+1<tail && get_up(q[head+1],q[head])<=sum[i]*get_down(q[head+1],q[head])) head++; dp[i]=get_dp(i,q[head]); while(head+1<tail && get_up(i,q[tail-1])*get_down(q[tail-1],q[tail-2])<=get_up(q[tail-1],q[tail-2])*get_down(i,q[tail-1])) tail--; q[tail++]=i; } printf("%d\n",dp[n]); } return 0; }
原文:http://www.cnblogs.com/L-Memory/p/7206544.html