标签:des style blog http java color
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 4990 Accepted Submission(s): 1509
1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 #include <string.h> 5 #include <set> 6 using namespace std; 7 int a[600000],sum[600000],h[600000],dp[600000],m; 8 int getup(int j,int k) 9 { 10 return dp[j]+sum[j]*sum[j]-dp[k]-sum[k]*sum[k]; 11 } 12 int getdown(int j,int k) 13 { 14 return ((sum[j]-sum[k])<<1); 15 } 16 int getdp(int i,int j) 17 { 18 return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]); 19 } 20 int main() 21 { 22 int n,i,head,tail; 23 while(~scanf("%d%d",&n,&m)) 24 { 25 memset(h,0,sizeof(h)); 26 sum[0]=a[0]=0; 27 for(i=1; i<=n; i++) 28 scanf("%d",&a[i]),sum[i]=sum[i-1]+a[i]; 29 head=tail=0; 30 h[tail++]=0; 31 for(i=1;i<=n;i++) 32 { 33 while(head+1<tail&&getup(h[head+1],h[head])<=sum[i]*getdown(h[head+1],h[head])) 34 head++; 35 dp[i]=getdp(i,h[head]); 36 while(head+1<tail&&getup(h[tail-1],h[tail-2])*getdown(i,h[tail-1])>=getup(i,h[tail-1])*getdown(h[tail-1],h[tail-2])) 37 tail--; 38 h[tail++]=i; 39 } 40 cout<<dp[i-1]<<endl; 41 } 42 }
Print Article hdu 3507 一道斜率优化DP 表示是基础题,但对我来说很难,布布扣,bubuko.com
Print Article hdu 3507 一道斜率优化DP 表示是基础题,但对我来说很难
标签:des style blog http java color
原文地址:http://www.cnblogs.com/ERKE/p/3833612.html