HDU 4283 You Are the One【区间DP】

来源:互联网 发布:java游戏代码大全 编辑:程序博客网 时间:2024/06/18 18:28

You Are the One

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

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 4268 4269 4270 4271

题意:给你一个队列,每个人有一个愤怒基数,他的愤怒值取决于他前面有多少个人。你可以通过一个小黑屋在一定程度上来更改这个队列的顺序,使得最后的总愤怒值最低。

分析:显然最优的情况是愤怒基数从大到小排,但这并不是一个小黑屋所能完成的。那么考虑到这个题可能是一个区间DP 问题。

对于每一个子区间,我们选择把当前区间【L,R】,把第一个关进小黑屋,关到K时释放。换句话说,原本最前面的那个人,现在第K个出来。我们相当于是:
(sum[R]sum[k])(kL+1)+(kL)a[L]+dp[L+1][k]+dp[k+1][R]
后面三项很好理解,最前面那一项是补个差价,琢磨琢磨就好。

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;const int maxn = 1e3 + 10;#define INF 0x3f3f3f3fint a[maxn];int sum[maxn];int dp[maxn][maxn];int cmp(int a, int b){    return a > b;}int main(){    int T;    scanf("%d", &T);    int n;    int cs = 0;    while (T--)    {        cs++;        memset(a, 0, sizeof a);        memset(sum, 0, sizeof sum);        scanf("%d", &n);        for (int i = 1; i <= n; i++)        {            scanf("%d", &a[i]);            sum[i] = sum[i - 1] + a[i];        }        //memset(dp, INF, sizeof dp);        //sort(a + 1, a + n + 1,cmp);        for (int i = 1; i <= n; i++)        {            for (int j = i + 1; j <= n; j++)            {                dp[i][j] = INF;            }        }        for (int i = 1; i <= n; i++)        {            for (int j = 1; j <= n; j++)            {                int L = j;                int R = j + i - 1;                for (int k = L; k <= R; k++)                {                    int tmp = (sum[R]-sum[k])*(k-L+1) + (k-L)*a[L] + dp[L+1][k] + dp[k + 1][R];                    dp[L][R] = min(dp[L][R], tmp);                }            }        }        printf("Case #%d: ", cs);        printf("%d\n", dp[1][n]);    }    return 0;}
原创粉丝点击