zoukankan      html  css  js  c++  java
  • HDU4258+DP+斜率优化

    对于斜率优化的dp还不是很懂。。。。。。。。。

    View Code
     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...
  • 相关阅读:
    品鉴-宋词
    【转载】全球水质最棒的十大景点
    Python文档管理与格式化工具
    Python音频处理
    Python剪切板提取、截图、图片粘贴,操作汇总
    Python多进程
    Wifi配置
    条码生成与解析
    谎言: “太空能看到的惟一的人工痕迹,长城!”
    VNC-Server安装配置详解
  • 原文地址:https://www.cnblogs.com/xxx0624/p/2998740.html
Copyright © 2011-2022 走看看