기울기 dp

3951 단어 HDU
사고방식: dp[i]=dp[j]+(sum[i]-sum[j])^2+m;
나머지는 일반 경사율 dp 방법입니다.
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<cmath>

#define Maxn 500010

#define LL __int64

using namespace std;

LL dp[Maxn],num[Maxn],sum[Maxn];

int que[Maxn*2];

LL getleft(int j,int k)

{

    return dp[j]+sum[j]*sum[j]-(dp[k]+sum[k]*sum[k]);

}

LL getright(int j,int k)

{

    return 2*(sum[j]-sum[k]);

}

int main()

{

    int n,m,i,j,head,rear;

    while(scanf("%d%d",&n,&m)!=EOF){

        memset(dp,0,sizeof(dp));

        for(i=1;i<=n;i++){

            scanf("%I64d",num+i);

            sum[i]=sum[i-1]+num[i];

        }

        head=1,rear=0;

        que[++rear]=0;

        for(i=1;i<=n;i++){

            while(head<rear&&getleft(que[head+1],que[head])<sum[i]*getright(que[head+1],que[head]))

                head++;

            dp[i]=dp[que[head]]+(sum[i]-sum[que[head]])*(sum[i]-sum[que[head]])+m;

            while(head<rear&&getleft(i,que[rear])*getright(que[rear],que[rear-1])<=getleft(que[rear],que[rear-1])*getright(i,que[rear]))

                rear--;

            que[++rear]=i;

        }

        printf("%I64d
",dp[n]); } return 0; }

좋은 웹페이지 즐겨찾기