HDU 4283 You Are the One(区间DP)

来源:互联网 发布:c语言异或符号 编辑:程序博客网 时间:2024/05/29 03:23

Description

  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
 
题意:n个人顺序出场。有一个密室可以藏人改变出场循序。问最大不高兴值。

题解:改变出场循序之后可以最小化不高兴值。
区间DP:设dp[i][j]为i~j的人出场的最小不高兴值。
然后第i个人可以选择第k个出场。代价就是前面k个人出场+这个人出场+后面人出场。
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int INF = 0x3f3f3f3f;int t,n;int a[110],sum[110];int dp[110][110];int main(){    int cas=1,temp;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        sum[0]=0;        REPF(i,1,n)        {          scanf("%d",&a[i]);          sum[i]=sum[i-1]+a[i];        }        CLEAR(dp,0);        for(int i=n-1;i>=1;i--)        {            for(int j=i+1;j<=n;j++)            {                dp[i][j]=INF;                for(int k=i;k<=j;k++)//第k个出场                    dp[i][j]=min(dp[i][j],dp[i+1][k]+(k-i)*a[i]+(sum[j]-sum[k])*(k-i+1)+dp[k+1][j]);            }        }        printf("Case #%d: %d\n",cas++,dp[1][n]);    }    return 0;}/**/


0 0