hdu 4283 You Are the One
You Are the One
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1217 Accepted Submission(s): 572
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
1 //15MS 272K 1037 B C++ 2 /* 3 4 题意: 5 有一群屌丝,每个屌丝有个屌丝值,如果他第K个上场, 6 屌丝的愤怒值就为a[i]*(k-1),通过一个小黑屋(堆栈)来调整,求 7 最后最小的总愤怒值 8 9 区间DP: 10 dp[i][j]表示从第i个人到第j个人这段区间的最小花费 11 (是只考虑这j-i+1个人,不需要考虑前面有多少人) 12 13 对于dp[i][j]的第i个人,就有可能第1个上场,也可以第j-i+1个上场。 14 考虑第K个上场,即在i+1之后的K-1个人是率先上场的,那么就出现了一个 15 子问题 dp[i+1][i+k-1]表示在第i个人之前上场的,对于第i个人,由于是 16 第k个上场的,那么屌丝值便是a[i]*(k-1);其余的人是排在第k+1个之后 17 出场的,也就是一个子问题dp[i+k][j],对于这个区间的人,由于排在 18 第k+1个之后,所以整体愤怒值要加上k*(sum[j]-sum[i+k-1]) 19 20 21 */ 22 #include<stdio.h> 23 #include<string.h> 24 #define inf 0x7ffffff 25 #define N 105 26 int dp[N][N]; 27 int sum[N]; 28 int a[N]; 29 int Min(int a,int b) 30 { 31 return a<b?a:b; 32 } 33 int main(void) 34 { 35 int t,n; 36 int k=1; 37 scanf("%d",&t); 38 while(t--) 39 { 40 scanf("%d",&n); 41 memset(sum,0,sizeof(sum)); 42 memset(dp,0,sizeof(dp)); 43 for(int i=1;i<=n;i++) 44 for(int j=i+1;j<=n;j++) 45 dp[i][j]=inf; 46 sum[0]=0; 47 for(int i=1;i<=n;i++){ 48 scanf("%d",&a[i]); 49 sum[i]=sum[i-1]+a[i]; 50 } 51 for(int i=1;i<n;i++) //区间宽 52 for(int j=1;i+j<=n;j++){ //第j~t个出场的情况 53 int t=i+j; 54 for(int k=1;k<=i+1;k++){//第j个人排在第k位 55 dp[j][t]=Min(dp[j][t],dp[j+1][j+k-1]+dp[j+k][t]+a[j]*(k-1)+k*(sum[t]-sum[j+k-1])); 56 } 57 //printf("dp[%d][%d]==%d\n",j,t,dp[j][t]); 58 } 59 printf("Case #%d: %d\n",k++,dp[1][n]); 60 } 61 return 0; 62 }