标签:
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)5 5 5 9 5 7 5
230
#include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<iostream> #include<algorithm> #include<bitset> #include<climits> #include<list> #include<iomanip> #include<stack> #include<set> using namespace std; int sum[500010],dp[500010],q[500010]; int gety(int i,int j) { return dp[i]-dp[j]+(sum[i]+sum[j])*(sum[i]-sum[j]); } int getx(int i,int j) { return 2*(sum[i]-sum[j]); } int main() { int n,m; while(cin>>n>>m) { for(int i=1;i<=n;i++) { cin>>sum[i]; sum[i]+=sum[i-1]; } int head(0),tail(1); for(int i=1;i<=n;i++) { while(head+1<tail&&gety(q[head+1],q[head])<=sum[i]*getx(q[head+1],q[head])) head++; dp[i]=dp[q[head]]+(sum[i]-sum[q[head]])*(sum[i]-sum[q[head]])+m; while(head+1<tail&&gety(i,q[tail-1])*getx(q[tail-1],q[tail-2])<=getx(i,q[tail-1])*gety(q[tail-1],q[tail-2])) tail--; q[tail++]=i; } cout<<dp[n]<<endl; } }
标签:
原文地址:http://blog.csdn.net/stl112514/article/details/44966643