hdu4281 区间dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4283
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; const int maxn = 105; 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=1;cas<=T;cas++){ cin>>N; sum[0] = 0; for(int i=1;i<=N;i++){ scanf("%d",&D[i]); sum[i] = sum[i-1] + D[i]; } memset(dp,0x3f,sizeof(dp)); for(int i=0;i<=N;i++) dp[i+1][i] = 0,dp[i][i] = 0; for(int i=N;i>=1;i--) for(int j=i+1;j<=N;j++){ for(int k=1;k<=j-i+1;k++){ dp[i][j] = min(dp[i][j],dp[i+1][i+k-1]+D[i]*(k-1)+dp[i+k][j]+k*(sum[j]-sum[i+k-1])); } } printf("Case #%d: %d\n",cas,dp[1][N]); } return 0; }