HDU

来源:互联网 发布:品类管理数据分析 编辑:程序博客网 时间:2024/06/07 15:16
  The TV shows such as You Are the One has been very popular. In order to meet the need of boys who are still single, TJUT hold the show itself. The show is hold in the Small hall, so it attract a lot of boys and girls. Now there are n boys enrolling in. At the beginning, the n boys stand in a row and go to the stage one by one. However, the director suddenly knows that very boy has a value of diaosi D, if the boy is k-th one go to the stage, the unhappiness of him will be (k-1)*D, because he has to wait for (k-1) people. Luckily, there is a dark room in the Small hall, so the director can put the boy into the dark room temporarily and let the boys behind his go to stage before him. For the dark room is very narrow, the boy who first get into dark room has to leave last. The director wants to change the order of boys by the dark room, so the summary of unhappiness will be least. Can you help him? 
Input
  The first line contains a single integer T, the number of test cases.  For each case, the first line is n (0 < n <= 100) 
  The next n line are n integer D1-Dn means the value of diaosi of boys (0 <= Di <= 100) 
Output
  For each test case, output the least summary of unhappiness . 
Sample Input
2  512345554322
Sample Output
Case #1: 20Case #2: 24


#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>using namespace std;typedef long long LL;typedef long double LD;const int inf = 0x3f3f3f3f;const LL INF = 0x3f3f3f3f3f3f3f3fll;const int maxn = 100+10;int T,N;int value[maxn];int dp[maxn][maxn];int sum[maxn];int main(){scanf("%d",&T);for(int t = 1; t <= T; t++){scanf("%d",&N);memset(sum,0,sizeof(sum));for(int i = 1; i <= N; i++)for(int j = 1; j <= N; j++){if(j<=i)dp[i][j] = 0;elsedp[i][j] = inf;}for(int i = 1; i <= N; i++){scanf("%d",&value[i]);sum[i] = sum[i-1]+value[i];//维护前缀和}for(int l = 1; l <= N; l++){//区间长度for(int i = 1; i+l-1 <= N; i++){//区间开头int j = i+l-1;//区间结尾for(int k = 1; k <= l; k++){//第i个人可以在(k的范围内)上场dp[i][j] = min(dp[i][j],dp[i+1][i+k-1]+dp[i+k][j]+value[i]*(k-1)+k*(sum[j]-sum[i+k-1]));//dp[i+1][i+k-1] -- 比第i个人先上场的(k-1)个人所转化的子问题//dp[i+k][j] -- 比第i个人后上场的(j-i+1-k)个人所转化的子问题//k*(sum[j]-sum[i+k-1]) -- 比第i个人后上场的(j-i+1-k)个人所贡献的屌丝值//value[i]*(k-1) -- 第i个人上场后所贡献的屌丝值}}}printf("Case #%d: %d\n",t,dp[1][N]);}return 0;}