1. 程式人生 > >HDU4283(KB22-G)

HDU4283(KB22-G)

icpc test ava first 單獨 title summary pre single

You Are the One

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


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

dp[i][j]表示區間[i,j]的最小總不開心值

把區間[i,j]單獨來看,則第i個人可以是第一個出場,也可以是最後一個出場(j-i+1),也可以是在中間出場(1 ~ j-i+1)

不妨設他是第k個出場的(1<=k<=j-i+1),那麽根據棧後進先出的特點,以及題目要求原先男的是排好序的,那麽::

第 i+1 到 i+k-1 總共有k-1個人要比i先出棧,

第 i+k 到j 總共j-i-k+1個人在i後面出棧

舉個例子吧:

有5個人事先排好順序 1,2,3,4,5

入棧的時候,1入完2入,2入完3入,如果我要第1個人第3個出場,那麽入棧出棧順序是這樣的:

1入,2入,3入,3出,2出,1出(到此第一個人就是第3個出場啦,很明顯第2,3號人要在1先出,而4,5要在1後出)

這樣子, 動態轉移方程 就出來了,根據第i個人是第k個出場的,將區間[i,j]分成3個部分

dp[i][j]=min(dp[i][j],dp[i+1,i+k-1]+dp[i+k,j]+(k-1)*a[i]+(sum[j]-sum[i+k-1])*k);

(sum[j]-sum[i+k-1])*k 表示 後面的 j-i-k+1個人是在i後面才出場的,那麽每個人的不開心值都會加個 unhappy,sum[i]用來記錄前面i個人的總不開心值,根據題目,每個人的unhappy是個 累加的過程 ,多等一個人,就多累加一次

 1 //2017-05-23
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 const int N = 110;
10 const int inf = 0x3f3f3f3f;
11 int D[N], dp[N][N], sum[N];
12 
13 int main()
14 {
15     int T, n;
16     scanf("%d", &T);
17     for(int kase = 1; kase <= T; kase++){
18         scanf("%d", &n);
19         for(int i = 0; i < n; i++){
20               scanf("%d", &D[i]);
21             if(i == 0)sum[i] = D[i];
22             else sum[i] = sum[i-1] + D[i];
23         }
24         memset(dp, 0, sizeof(dp));
25         for(int i = 0; i < n; i++)
26               for(int j = i+1; j < n; j++)
27                   dp[i][j] = inf;
28         for(int len = 1; len <= n; len++){
29             for(int l = 0; l+len <= n; l++){
30                 int r = l+len-1;
31                 for(int k = 1; k <= len; k++){
32                     dp[l][r] = min(dp[l][r], dp[l+1][l+k-1]+dp[l+k][r]+(k-1)*D[l]+(sum[r]-sum[l+k-1])*k);
33                 }
34             }
35         }
36         printf("Case #%d: %d\n", kase, dp[0][n-1]);
37     }
38 
39     return 0;
40 }

HDU4283(KB22-G)