HDU 4283 You Are the One(区间DP)

来源:互联网 发布:阿里云服务器客服 编辑:程序博客网 时间:2024/06/05 15:21

  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

 【题解】 

 大致题意:有那个人要出场表演,但是每个人都有一个屌丝值,这会影响表演的效果,所以现在要让整个演出的屌丝值最少。

 这里有一个房间,房间太窄了,不想先表演的人可以进入房间中,让身后的人先上,但是这个房间是进去早的人出来的晚,就相当于一个栈结构。

 第i个人在第k个出场的话,那么获得的屌丝值就是(k-1)*a[i],求最小屌丝总值。


 分析: 区间dp吧,就是栈这里比较难处理,要分为两个区间,用dp[i][j]表示第i个人到第j个人出场的最小屌丝值,那么第i个人的出场顺序为k,那么k之前的人出场顺序不会因i 而改变,所以前面每个人的屌丝值就是它的出场顺序-1乘以a[i],而i后面的人的出场顺序则因i而改变,而变化的屌丝值就是(sum[k]-sum[j+l-1])*l,所以方程就是:

dp[j][k]=min(dp[j][k],dp[j+1][j+l-1]+(l-1)*a[j]+dp[j+l][k]+l*(sum[k]-sum[j+l-1]));


 【AC代码】

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N=105;const int inf=1e9;int m,n;int dp[N][N];int a[N],sum[N];int solve(){    for(int i=1;i<m;++i)//初始化        for(int j=i+1;j<=m;++j)        dp[i][j]=inf;    for(int i=1;i<m;++i)//区间递进变量        for(int j=1;j<=m-i;++j)//区间始  枚举第j个人进入出场        {            int k=i+j;//区间终            for(int l=1;l<=k-j+1;++l)//第j个人第l个出场                dp[j][k]=min(dp[j][k],dp[j+1][j+l-1]+(l-1)*a[j]+dp[j+l][k]+l*(sum[k]-sum[j+l-1]));        }//此处把区间分为两部分,一部分j+1到j+l-1 是在他之前上场的,不受他影响  ,而j+l到k是在他之后上场的         //因为他,后面的人会的次序变了,变化产生的沮丧值就是l*(sum[k]-sum[j+l-1]);    return dp[1][m];}int main(){    int t;    scanf("%d",&t);    for(int i=1;i<=t;++i)    {        scanf("%d",&m);        sum[0]=0;        for(int j=1;j<=m;++j)        {            scanf("%d",&a[j]);            sum[j]=sum[j-1]+a[j];        }        printf("Case #%d: %d\n",i,solve());    }    return 0;}


原创粉丝点击