• You Are the One HDU


    题目描述

     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 .

    题目大意 :每组输入N,表示有N个人要上台表演,然后输入这N个人的不开心度。如果第i个人第k个上台表演,那么他的不开心度就是d[i]*(k-1)。现在有一个狭窄的小黑屋,导演可以暂时让人先进入小黑屋中,然后让后面的人去舞台表演,因为小黑屋很狭窄所以最先进去的人最后才能出来,问怎么安排才能使所有人的不开心度最小,输出这个不开心度。

    解题思路:用区间dp做,首先dp[ i ][ j ]表示在从 i 到 j 的区间内的最小值。

                      首先,假设第 i 个人是在第 k 个进入的,所以这个人的不开心度就是 k - 1)*d[ i ]

                      i 前面的人的不开心度是 dp [ i + 1][ i + k -1]

                      i 后面的人的不开心度是 dp[ i + k][ j ] ,又由于这些人前面有k个人,因此需要把这些人的 d [ x ] 的总和乘以 k ,因此这一            段的不开心值就是  dp[ i + k][ j ] + k * (sum[ j ] - sum[ i + k - 1] )  。  

                     最后得出状态转移方程为

                     dp[ i ][ j ] = min ( dp[ i ][ j ] , dp[ i + 1 ][ i + k - 1]+ (k - 1)*d[ i ] +dp[ i + k][ j ] + k * (sum[ j ]  - sum[ i + k - 1] ))

    代码: 

    #include<bits/stdc++.h>
    #define ll long long
    #define MOD 998244353 
    #define INF 0x3f3f3f3f
    #define mem(a,x) memset(a,x,sizeof(a))  
    #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    using namespace std;
    int w[105];
    int dp[105][105];
    int sum[105];
    int n;
    int main()
    {
        int t,CASE;
        scanf("%d",&t);
        CASE=t;
        while(t--){
            scanf("%d",&n);
            mem(sum,0);
            for(int i=1;i<=n;i++){
                scanf("%d",&w[i]);
                sum[i]=sum[i-1]+w[i];
            }
            mem(dp,0);
            for(int len=1;len<=n;len++){
                for(int i=1;i<=n-len+1;i++){
                    int j=i+len-1;
                    dp[i][j]=INF;
                    for(int k=1;k<=len;k++){
                        dp[i][j]=min(dp[i][j],dp[i+1][i+k-1]+dp[i+k][j]+w[i]*(k-1)+k*(sum[j]-sum[i+k-1]));
                    }
                }
            }
            printf("Case #%d: %d
    ",CASE-t,dp[1][n]);
        }
        return 0;
    }
    越自律,越自由
  • 相关阅读:
    Viewpager的用法
    Android上下文菜单
    Android开机自启动程序
    微信小程序踩过的坑
    Sublime Text 配置python文件
    Pandas速查手册中文版
    scrapy架构设计分析
    爬虫服务集群处理nginx返回504
    phantomjs 无法打开https网站
    Selenium判断获取的元素是否可见(display:none)
  • 原文地址:https://www.cnblogs.com/ha-chuochuo/p/13435563.html
Copyright © 2020-2023  润新知