• You Are the One


    You Are the One
    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
    

    首先,小黑屋是个栈,并且出栈之后只能是上台演出,不能回到队伍。
    所以,当屋里只有一个人时,要么这个人上台,要么队伍第一个人上。
    队伍就能划分成三块:
    $

    1. (a_i) 前面上台的(可能是0个)
    2. (a_i)上台
    3. (a_i)后面上台的(可能是0个)

    第一块显然就是子问题
    第二块只需计算第一块里有多少人就行了
    第三块也能变成子问题:
    设a_i 前面有x个人,设j=i+1
    (xa_j+(x+1)a_{j+1}+(x+2)a_{j+2}dots(x+n)a_{j+n}=0a_j+(0+1)a_{j+1}+(0+2)a_{j+2}dots(0+n)a_{j+n}+x*sum a_j)

    所以对于i<=k<=j
    (dp[i][j]=min(dp[i][k]+a[i]*(k-i)+dp[k+1][j]+(k-i+1)*(s[j]-s[k])))

    #include<bits/stdc++.h>
    using namespace std;
    #define Init(arr,val) memset(arr,val,sizeof(arr))
    const int inf=0x3f3f3f3f,mod=1000000007,MAXN=10;
    int n,a[MAXN],s[MAXN],dp[MAXN][MAXN];
    int main(){
        int t,cas=1;
        scanf("%d",&t);
        while(t--){
            scanf("%d",&n);
            for(int i=1;i<=n;++i){scanf("%d",a+i);s[i]=a[i]+s[i-1];}
            Init(dp,0);
            for(int len=2;len<=n;++len){
                for(int i=1,j=len;j<=n;++i,++j){
                    dp[i][j]=inf;
                    for(int k=i;k<=j;++k){
                        dp[i][j]=min(dp[i][j],
                        dp[i+1][k]+a[i]*(k-i)+dp[k+1][j]+(k-i+1)*(s[j]-s[k]));
                    }
                }
            }
            printf("Case #%d: %d
    ",cas++,dp[1][n]);
        }
        return 0;
    }
    
  • 相关阅读:
    05用户故事与敏捷方法笔记之五
    04用户故事与敏捷方法笔记之四
    03用户故事与敏捷方法笔记之三
    框架学习.关于内省api操作bean属性
    02用户故事与敏捷方法笔记之二
    01用户故事与敏捷方法笔记之一
    问题账户需求分析
    2017年秋季个人阅读计划
    第二冲刺项目进展
    典型用户与场景
  • 原文地址:https://www.cnblogs.com/foursmonth/p/14155848.html
Copyright © 2020-2023  润新知