(HDu 4283)You Are the One(区间DP)

You Are the One

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4835    Accepted Submission(s): 2306


Problem 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    5 1 2 3 4 5 5 5 4 3 2 2
 

 

Sample Output
Case #1: 20 Case #2: 24
 
 
将n个男嘉宾重新排序出场(使用一个栈进行维护),第k个出场的人会有a【i】*(k-1)的厌恶值。
先不管使用栈的情况,dp【i】【j】表示从i到j的人出场的最低厌恶值(1~i-1和j+1~n的顺序不变);
 
在计算dp【i】【j】时,枚举k,i<=k<=j,k的意义是第i个人的出场次序(即标号为i的人第k个出场)。
这样一来在【i,j】区间内,从i+1到k的人要在i之前出场,k+1到j的人要在i之后出场
即转化成两个子问题:dp【i+1】【k】和dp【k+1】【j】;
所以dp【i】【j】=min(a[i]*(k-1)+dp[i+1][k]-(sum[k]-sum[i])+dp[k+1][j])
注:因为dp【i+1】【k】整体向前挪了一位所以要减去一倍的和(上式中用前缀和表示,详见代码)
最后dp【1】【n】即为答案
 
#include<iostream>
#include<cstdio>
#include<vector>
#include<set>
#include<map>
#include<string.h>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f

using namespace std;

int a[120];
int b[120];
int dp[120][120];

int main()
{
    int t;
    scanf("%d",&t);
    for(int p=1;p<=t;p++)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(dp,0,sizeof(dp));
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[i]=a[i]+b[i-1];
        }
        for(int l=0;l<n;l++)
            for(int i=1;i+l<=n;i++)
            {
                int j=i+l;
                if(l==0)
                {
                    dp[i][j]=a[i]*(i-1);
                    continue;
                }
                dp[i][j]=inf;
                for(int k=i;k<=j;k++)
                {
                    dp[i][j]=min(dp[i][j],a[i]*(k-1)+dp[i+1][k]-b[k]+b[i]+dp[k+1][j]);
                }
                //printf("dp%d%d=%d\n",i,j,dp[i][j]);
            }
        printf("Case #%d: %d\n",p,dp[1][n]);
    }
    return 0;
}

 

posted @ 2018-03-04 10:56  海哥天下第一  阅读(173)  评论(0编辑  收藏  举报