noi1999 石子合并

区间dp模板题

外面一层控制长度

内层控制左端点l

最内层控制切点k

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

typedef long long ll;
const int maxn=1000;

ll a[maxn];
ll dp[maxn][maxn];
ll sum[maxn];

int main(){
    int n;
    scanf("%d",&n);
    for (int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
    }
    memset(dp,0x3f,sizeof(dp));
    for (int i=1;i<=n;i++){
        dp[i][i]=0;
        sum[i]=sum[i-1]+a[i];
    }
    ll ans=0x3f3f3f3f;
    for(int len=2;len<=n;len++){
        for (int l=1;l<=n-len+1;l++){
            int r=l+len-1;
            for (int k=l;k<r;k++){
                dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]+sum[r]-sum[l-1]);
            }
        }
    }
    printf("%lld\n",dp[1][n]);
return 0;
}

 

posted @ 2018-08-08 19:13  lmjer  阅读(132)  评论(0编辑  收藏  举报