首页 > 代码库 > HDU 4283 You Are the One(区间dp)

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): 1743    Accepted Submission(s): 829


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
 

Source
2012 ACM/ICPC Asia Regional Tianjin Online
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:  4267 4269 4270 4271 4272 
 

题意:
有一群屌丝,每个屌丝有个屌丝值,如果他第K个上场,屌丝值就为a[i]*(k-1),同过一个小黑屋来调整,是的最后总屌丝值最小。
(注意:顺序必须在原来顺序的基础上改,要按原来的顺序选择是进小黑屋还是上场,不能简单的从大到小排序,再计算)。

题解:
区间dp,枚举区间长度,在这段区间中,dp[i][j]表示i 在[i,j]中第k个上场的最优解。
dp[i][j] = min ( dp[i][j], dp[i + 1][k + i - 1] + dp[i + k][j] + k * ( sum[j] - sum[i + k - 1] ) + a[i] * ( k - 1 ) )

AC Code;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define N 100010
#define Mod 10000007
#define lson l,mid,idx<<1
#define rson mid+1,r,idx<<1|1
#define lc idx<<1
#define rc idx<<1|1
const double EPS = 1e-11;
const double PI = acos ( -1.0 );
const double E = 2.718281828;
typedef long long ll;

const int INF = 100000010;

using namespace std;

int a[111], n;
int dp[111][111];
int sum[111];

int main()
{
    int t;
    while ( cin >> t )
    {
        int ca = 1;
        while ( t-- )
        {
            cin >> n;
            sum[0] = 0;
            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 l = 1; l <= n; l++ )
            {
                for ( int i = 1, j = i + l - 1; j <= n; j++, i++ )
                {
                    dp[i][j] = INF;
                    for ( int k = 1; k <= l; k++ )
                    {
                        dp[i][j] = min ( dp[i][j], dp[i + 1][k + i - 1] + dp[i + k][j] + k * ( sum[j] - sum[i + k - 1] ) + a[i] * ( k - 1 ) );
                    }
                }
            }
            printf ( "Case #%d: %d\n", ca++, dp[1][n] );
        }
    }
    return 0;
}
/*
2
5
1 2 3 4 5

5
5 4 3 2 2
*/


HDU 4283 You Are the One(区间dp)