You Are the One

HDU - 4283

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 .

题意:

有n个人要上台,每个人都有一个值。当某人是第k个上台时,那么他的不开心值等于(k-1)*d,现在有一个栈一样的房间,对于每个人可以让他上台或者进入房间等待,求所有人的不开心值的最小值。

思路:

区间dp,dp数组的意义就是从i到j个人的最小的不开心值。
而策略就是对于区间 [i,j] 将第i个人,假设为第k个上场的,那么根据栈的原理,从i+1到i+k-1个人是在i前面上场,从i+k到j个人是在i后面上场。
dp[i][j]=min( dp[i][j] , dp[i+1][i+k-1]+dp[i+k][j]+(k-1)*D[i]+(sum[j]-sum[i+k-1])*k);
对于后面这一部分,因为从i+k到j个人的前面每插入一个人,每一位的不开心值的倍数都要++,所以前面插入几个人就要把后面的sum给乘几。

代码

#include
using namespace std;
typedef long long ll;
int sum[110],a[110],dp[110][110];
int main()
{
    int T;
    cin>>T;
    for(int t=1;t<=T;t++){
        int n;
        cin>>n;
        memset(dp,0,sizeof(dp));
        memset(sum,0,sizeof(sum));
        
        for(int i=1;i<=n;i++){
        	cin>>a[i];
            sum[i]=sum[i-1]+a[i];
        }
        for(int k=1;k

你可能感兴趣的:(ACM,区间DP)