标签:des style blog http color os io java strong
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
1 #include<cstdio> 2 #include<cstdlib> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 const int N = 500100; 7 #define For(i,n) for(int i=1;i<=n;i++) 8 #define Rep(i,l,r) for(int i=l;i<=r;i++) 9 10 int n,M,sum[N],dp[N],q[N]; 11 // dp[i] = max{dp[j] + (sj-si)^2 + M} 12 //-> dp[k] - dp[j] + sk^2 - sj^2 13 // -------------------------- > 2si 时 j比k优 14 // sk - sj 15 16 int Up(int i,int j){ 17 return dp[i]+sum[i]*sum[i]-(dp[j]+sum[j]*sum[j]); 18 } 19 20 int Down(int i,int j){ 21 return sum[i]-sum[j]; 22 } 23 24 void DP(){ 25 int l=0,r=1; 26 For(i,n){ 27 while(l+1<r && Up(q[l],q[l+1])>=2*sum[i]*Down(q[l],q[l+1])) l++; 28 dp[i]=dp[q[l]] + (sum[i]-sum[q[l]])*(sum[i]-sum[q[l]]) + M; 29 while(l+1<r && Up(q[r-2],q[r-1])*Down(q[r-1],i)>=Up(q[r-1],i)*Down(q[r-2],q[r-1])) --r; 30 q[r++]=i; 31 } 32 printf("%d\n",dp[n]); 33 } 34 35 int main(){ 36 while(scanf("%d%d",&n,&M)!=EOF){ 37 q[0]=dp[0]=0; 38 For(i,n){ 39 scanf("%d",&sum[i]); 40 sum[i]+=sum[i-1]; 41 } 42 DP(); 43 } 44 return 0; 45 }
标签:des style blog http color os io java strong
原文地址:http://www.cnblogs.com/kjerome/p/3951107.html