HDU4258+DP+斜率优化
对于斜率优化的dp还不是很懂。。。。。。。。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 dp+斜率优化 3 */ 4 #include<stdio.h> 5 #include<string.h> 6 #include<stdlib.h> 7 #include<algorithm> 8 #include<iostream> 9 #include<queue> 10 #include<math.h> 11 using namespace std; 12 const int maxn = 1000005; 13 const int cl = 0x7F; 14 typedef __int64 int64; 15 int64 a[ maxn ],dp[ maxn ]; 16 int que[ maxn ]; 17 double get( int64 x,int64 y ){ 18 return 1.0*( dp[x]-dp[y]+a[x+1]*a[x+1]-a[y+1]*a[y+1] )/( 2.0*(a[x+1]-a[y+1]) ); 19 } 20 void dp_solve( int64 n,int64 c ){ 21 memset( dp,cl,sizeof( dp ) ); 22 dp[ 0 ] = 0; 23 int head,tail; 24 head = tail = 1; 25 que[ 1 ] = 0; 26 for( int i=1;i<=n;i++ ){ 27 while( head<tail&&get( que[ head+1 ],que[ head ] )<=(double)a[ i ] ) 28 head++; 29 dp[ i ] = dp[ que[head] ]+c+( a[i]-a[ que[head]+1 ])*( a[i]-a[ que[head]+1 ] ); 30 while( head<tail&&get( i,que[ tail ] )<=get( que[ tail ],que[ tail-1 ] ) ) 31 tail--; 32 que[ ++tail ] = i; 33 } 34 printf("%I64d\n",dp[ n ]); 35 return ; 36 } 37 int main(){ 38 int64 n,c; 39 while( scanf("%I64d%I64d",&n,&c)==2 ){ 40 if( n==0 && c==0 ) break; 41 for( int i=1;i<=n;i++ ) 42 scanf("%I64d",&a[ i ]); 43 dp_solve( n,c ); 44 } 45 return 0; 46 }
keep moving...