uva10688 The Poor Giant(DP)

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=1629

水题记忆化dp

#include <iostream>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
int t,n,k;
ll a[1010];
ll dp[1100][1100];
ll DP(int x,int y){
   if(x+1==y) return 2*min(a[x],a[y]);
   if(x>=y) return 0;
   if(dp[x][y]!=-1) return dp[x][y];
   ll ret=1e17;
   for(int i=x;i<=y;i++){
      ret=min(ret,a[i]*(y-x+1)+DP(x,i-1)+DP(i+1,y));
   }
   return dp[x][y]=ret;
}
int main(){
    scanf("%d",&t);
    for(int ca=1;ca<=t;ca++){
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++) a[i]=k+i;
        memset(dp,-1,sizeof dp);
        printf("Case %d: %lld
",ca,DP(1,n));
    }
    return 0;
}
uva10688
原文地址:https://www.cnblogs.com/wonderzy/p/3541893.html