hdu4281 区间dp

时间:2021-12-06 09:22:09

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4283

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std; const int maxn = ;
const int INF = 0x3f3f3f3f; int dp[maxn][maxn];
//dp[i][j]表示只考虑编号为i到编号为j的人上场的最小不开心值;
//枚举i第K(1=<K<=j-i+1)个上场,dp[i][j] = dp[i+1][i+k-1] + D[i] * (k-1) + dp[i+k][j] + k*(sum[j]-sum[i+k-1]);
int sum[maxn];
int D[maxn];
int N; int main()
{
//freopen("E:\\acm\\input.txt","r",stdin);
int T;
cin>>T;
for(int cas=;cas<=T;cas++){
cin>>N;
sum[] = ;
for(int i=;i<=N;i++){
scanf("%d",&D[i]);
sum[i] = sum[i-] + D[i];
}
memset(dp,0x3f,sizeof(dp));
for(int i=;i<=N;i++) dp[i+][i] = ,dp[i][i] = ; for(int i=N;i>=;i--)
for(int j=i+;j<=N;j++){
for(int k=;k<=j-i+;k++){
dp[i][j] = min(dp[i][j],dp[i+][i+k-]+D[i]*(k-)+dp[i+k][j]+k*(sum[j]-sum[i+k-]));
}
}
printf("Case #%d: %d\n",cas,dp[][N]);
}
return ;
}