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 .


The meaning of problems:
there are n individuals to come to power, everyone has a value. When someone is k-th came to power, he is not happy is equal to (k-1) * d, now there is a stack of the same room, for everyone to let him on stage or into the waiting room, ask everyone not happy minimum value.


Ideas:
meaning the interval dp, dp array is not happy smallest value from i to j individuals.
The strategy is for the interval [i, J] the person i, assumed to be k-th play, then according to the principles of the stack, from i + 1 to i + k-1 person is playing in front of i from i + k j is to play back the individual 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);
for later in this section, since the i + k j to the front of each individual insert a person, an unhappy each value have multiple ++, so insert a few people in front of the back should sum to take a few.


Code

#include<bits/stdc++.h>
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<n;k++){
            for(int i=1,j=i+k;j<=n;i++,j++){
                dp[i][j]=1e7+5;
                for(int d=1;d<=k+1;d++){
                    dp[i][j]=min(dp[i][j],dp[i+1][i+d-1]+dp[i+d][j]+(d-1)*a[i]+(sum[j]-sum[i+d-1])*d);
                }
            }
		}
        cout<<"Case #"<<t<<": "<<dp[1][n]<<'\n';
    }
    return 0;
}

  


----------------
CSDN link: https: //blog.csdn.net/weixin_43880627/article/details/103623666

Guess you like

Origin www.cnblogs.com/jjmmboom/p/12075399.html