HDU 3507(斜率最佳化dp

來源:互聯網
上載者:User

標籤:

題目:每次選取連續的若干數位代價

要求選取雖有數位最小代價.

思路:基礎斜率dp題,題解見http://www.cnblogs.com/kuangbin/archive/2012/08/26/2657650.html

/** @author:  Cwind*/#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <map>#include <algorithm>#include <cstdio>#include <cstring>#include <cstdlib>#include <vector>#include <queue>#include <stack>#include <functional>#include <set>#include <cmath>using namespace std;#define IOS std::ios::sync_with_stdio (false);std::cin.tie(0)#define pb push_back#define PB pop_back#define bk back()#define fs first#define se second#define sq(x) (x)*(x)#define eps (1e-6)#define IINF (1<<29)#define LINF (1ll<<59)#define INF (1000000000)#define FINF (1e3)typedef long long ll;typedef unsigned long long ull;typedef pair<int,int> pii;typedef pair<ll,ll> P;const int maxn=5e5+3000;int n,m;int a[maxn];int Q[maxn];int sum[maxn];int dp[maxn];int front,back;int get1(int a,int b){    return dp[b]+sq(sum[b])-dp[a]-sq(sum[a]);}int get2(int a,int b){    return 2*(sum[b]-sum[a]);}int main(){    freopen("/home/files/CppFiles/in","r",stdin);    //freopen("test.in","r",stdin);    //freopen("test.out","w",stdout);    while(cin>>n>>m){        front=back=0;        for(int i=1;i<=n;i++){            scanf("%d",&a[i]);            sum[i]=sum[i-1]+a[i];        }        Q[back++]=0;        for(int i=1;i<=n;i++){            while(front+1<back&&get1(Q[front],Q[front+1])<=sum[i]*get2(Q[front],Q[front+1])){                 front++;            }            dp[i]=dp[Q[front]]+sq(sum[i]-sum[Q[front]])+m;            while(front+1<back&&get1(Q[back-2],Q[back-1])*get2(Q[back-1],i)>=get1(Q[back-1],i)*get2(Q[back-2],Q[back-1])){                back--;            }            Q[back++]=i;        }        printf("%d\n",dp[n]);    }    return 0;}
View Code

 

HDU 3507(斜率最佳化dp

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.