You Are the One(区间DP 好题)

Posted 1013star

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了You Are the One(区间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

题意:

一群屌丝要上台表演节目, 由于舞台太小,每次只允许一个人表演,所以屌丝们在后台排起了长长的队,每位屌丝还有个屌丝值ai,轮不到他他就难受,不高兴,如果前边有k个人,他的怒气值就为ai*k;后台呢,还有个小黑屋,可以临时把一个屌丝值小的屌丝关进去,让后边的人先上台,小黑屋里可以关任意数量的人,但是先进去的要最后出来;问通过小黑屋安排后怒气和的最小值。

题解:

  设现在区间[l,r]的人要上台表演,如果l位置的人第k个上场,即他前面会有[l+1,l+k-1]个人先他上场,那么第l位置的人的怒气值为a[l]*(k-1).而[l+1,l+k-1]区间的最优解为dp[l+1][l+k-1],第l位置后面的人有[l+k,r]每个人的怒气值会增加a[i]*k即总共增加了k*(sum[r]-sum[l+k-1]) 故当L第K个上台的时候,dp[L][R]=dp[L+1][L+K-1]+D[L]*k+dp[L+K][R]+K*(sum[R]-sum[L+K-1]); k的范围是1~r-l+1。

 1 import java.util.Scanner;
 2 
 3 public class Main { 
 4     static int casen,n,inf = 10000000;
 5     static int [][] dp ;
 6     static int [] a = new int [110];
 7     static int [] sum = new int [110];
 8     public static void main(String[] args) {
 9         Scanner cin = new Scanner(System.in);
10         casen = cin.nextInt();
11         int ca = 1;
12         while(casen-->0) {
13             n = cin.nextInt();
14             dp = new int [n+5][n+5];
15             for(int i=0;i<n+2;i++)
16                 for(int j=0;j<n+2;j++)
17                     dp[i][j] = 0;
18             for(int i=1;i<=n;i++) {
19                 a[i] = cin.nextInt();
20                 sum[i] = sum[i-1]+a[i];
21             }
22             for(int len=2;len<=n;len++) {
23                 for(int l=0;l+len-1<=n;l++) {
24                     int r=l+len-1;
25                     int tmp = inf;
26                     for(int k=1;k<=len;k++) {
27                         tmp = Math.min(tmp, dp[l+1][l+k-1]+a[l]*(k-1)+dp[l+k][r]+k*(sum[r]-sum[l+k-1]));
28                     }
29                     dp[l][r] = tmp;
30                 }
31             }
32             System.out.println("Case #"+ ca++ +": "+dp[1][n]);
33         }
34     }
35 }
36  

 






以上是关于You Are the One(区间DP 好题)的主要内容,如果未能解决你的问题,请参考以下文章

HDU4283:You Are the One(区间DP)

you are the one(区间dp)

hdu 4283 You Are the One 区间dp

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

(HDu 4283)You Are the One(区间DP)

HDU4283 You Are the One(区间dp)