zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    Java正则表达式入门1
    JAVA中正则表达式总结
    Java正则表达式入门
    java String.valueOf()的作用
    【HDOJ】3601 Coach Yehr’s punishment
    【HDOJ】4601 Letter Tree
    【HDOJ】3686 Traffic Real Time Query System
    【HDOJ】5296 Annoying problem
    【HDOJ】3553 Just a String
    【HDOJ】4426 Palindromic Substring
  • 原文地址:https://www.cnblogs.com/foursmonth/p/14155848.html
Copyright © 2011-2022 走看看