You Are the One

 
 

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

题意:区间DP的题意还真是没有一道好理解的,唉

有一群屌丝,每个屌丝有个屌丝值,如果他第K个上场,屌丝值就为a[i]*(k-1),同过一个小黑屋来调整,是的最后总屌丝值最小

注意 :这个小黑屋是个栈,男屌的顺序是排好了的,但是可以通过入栈出栈来改变男屌的出场顺序
解题思路 :(操度娘所知~度娘你好腻害)
dp[i][j]表示区间[i,j]的最小总不开心值
把区间[i,j]单独来看,则第i个人可以是第一个出场,也可以是最后一个出场(j-i+1),也可以是在中间出场(i   ~  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. #include <iostream>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #define maxn 110
  5. #define inf 0x7fffffff
  6. using namespace std;
  7. int dp[maxn][maxn];
  8. int sum[maxn];
  9. int a[maxn];
  10. int n;
  11. int main()
  12. {
  13. int t,iCase=1;
  14. scanf("%d",&t);
  15. while(t--){
  16. sum[0]=0;
  17. scanf("%d",&n);
  18. for(int i=1;i<=n;i++){
  19. scanf("%d",&a[i]);
  20. sum[i]=sum[i-1]+a[i];
  21. }
  22. memset(dp,0,sizeof dp);
  23. for(int i=1;i<=n;i++){
  24. for(int j=i+1;j<=n;j++){
  25. dp[i][j]=inf;
  26. }
  27. }
  28. for(int p=1;p<=n;p++){
  29. for(int i=1;i<=n-p+1;i++){
  30. int j=i+p-1;
  31. for(int k=1;k<=p;k++){
  32. dp[i][j] = min(dp[i][j],dp[i+1][i+k-1]+dp[i+k][j]+k*(sum[j]-sum[i+k-1])+a[i]*(k-1));
  33. /*dp[i][j]表示从第i个人到第j个人这段区间的最小花费(是只考虑这j-i+1个人,不需要考虑前面有多少人)
  34.  那么对于dp[i][j]的第i个人,就有可能第1个上场,也可以第j-i+1个上场。考虑第K个上场 即在i+1之后的K-1个人是率先上场的,那么就出现了一个子问题 dp[i+1][i+k-1]表示在第i个人之前上场的
  35.  对于第i个人,由于是第k个上场的,那么屌丝值便是a[i]*(k-1) 其余的人是排在第k+1个之后出场的,也就是一个子问题dp[i+k][j],对于这个区间的人,由于排在第k+1个之后,所以整体愤怒值要加上k*(sum[j]-sum[i+k-1])  
     */
  36. }
  37. }
  38. }
  39. printf("Case #%d: %d\n",iCase++,dp[1][n]);
  40. }
  41. return 0;
  42. }


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值