You Are the One HDU - 4283(区间DP)

You Are the One HDU - 4283

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

题意:

有一群屌丝,每个屌丝有个屌丝值,如果他第K个上场,屌丝值就为a[i]*(k-1),因为前面有k-1个人在他前面上台,可以把几个人拉进小黑屋来调整,可以把小黑屋当成栈因为先进去的人最后才能出来,使得最后总屌丝值最小

分析:

区间DP

定义 d p [ i ] [ j ] 表示原序列区间 [ i , j ] 的人的最小屌丝值

这样我们枚举一个中间变量k,设i在区间[i,j]中是第k个上场的

这样区间分成了两部分

d p [ i + 1 ] [ i + k 1 ] ( k 1 i ) , d p [ i + k ] [ j ]

也就是让 [ i , i + k 1 ] 这些人进入小黑屋,i先进的,那么他前面将有k-1个人,他是第k个上场的,之后再让后面的 [ i + k , j ] 这些人上场

这样很明显i等待了k-1个人屌丝值增长为 ( k 1 ) × a [ i ] ,而 [ i + k , j ] 这些人则等待了k个人,屌丝值增长为 k ×

因此状态转移公式为:

d p [ i ] [ j ] = m i n ( d p [ i ] [ j ] , d p [ i + 1 ] [ i + k 1 ] + d p [ i + k ] [ j ] + k × ( s u m [ j ] s u m [ i + k 1 ] ) + a [ i ] × ( k 1 ) )

sum[]记录了每个人屌丝值的前缀和

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
int n,a[105],sum[105],dp[105][105];

int main(){
    int t,cas = 0;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        memset(sum,0,sizeof(sum));
        for(int i = 1; i <= n; i++){
            scanf("%d",&a[i]);
            sum[i] = sum[i-1] + a[i];
        }
        memset(dp,0,sizeof(dp));
        for(int i = 1; i <= n; i++){
            for(int j = i + 1; j <= n; j++){
                dp[i][j] = INF;
            }
        }
        //注意初始化,dp[i][i]=0,其他长度的为INF
        for(int l = 1; l < n; l++){
            for(int i = 1; i + l <= n; i++){
                int j = i + l;
                for(int k = 1; k <= j - i + 1; k++){
                    dp[i][j] = min(dp[i][j],dp[i+1][i+k-1]+dp[i+k][j]+k*(sum[j]-sum[i+k-1])+a[i]*(k-1));
                }
            }
        }
        printf("Case #%d: %d\n",++cas,dp[1][n]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/81479291