zoukankan      html  css  js  c++  java
  • You Are the One 区间DP

      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

    include<iostream>
    #include<cstring>
    using namespace std;
    /*
    注意到栈里面的人随时间增加的不耐烦度是他们DS值的和乘以等待的时间(就是后面上台人的次数)
    */
    #define MAXN 103
    #define INF 0x3f3f3f3f
    int a[MAXN];
    int dp[MAXN][MAXN];//dp[i][j]表示从i到j的人不耐烦值
    int pre[MAXN];
    int tmp[MAXN][MAXN];
    int main()
    {
        int T,n;
        ios::sync_with_stdio(0);
        cin>>T;
        int cas = 1;
        while(T--)
        {
            memset(dp,INF,sizeof(dp));
            memset(tmp,0,sizeof(tmp));
            cin>>n;
            for(int i =1;i <= n;i++)
            {
                cin>>a[i];
                pre[i] = pre[i-1] + a[i];
            }
            for(int i=0;i<=n;i++)
                dp[i][i] = 0;
            for(int i=1;i<=n;i++)
            {
                for(int j = i-1;j>=1;j--)
                {
                    tmp[j][i] = tmp[j+1][i] + a[j]*(i-j);
                }
            }
            for(int j=1; j<=n; j++)
                for(int i=j-1; i>=1; i--)
                    for(int k=i; k<j; k++)
                    {
                        dp[i][j] = min(dp[i][j],dp[k+1][j] + tmp[i][k] + (pre[k] - pre[i-1])*(j-k));
                        dp[i][j] = min(dp[i][j],dp[i][k] + dp[k+1][j] + (pre[j]-pre[k])*(k - i + 1)); 
                    }
            cout<<"Case #" << cas++<< ": "<<dp[1][n]<<endl;
        }
    }
  • 相关阅读:
    ( 三十一 )、 SpringBoot项目启动自动执行sql脚本
    转:OAuth 2.0 介绍
    采坑:python base64
    Redis学习——数据结构介绍(四)
    美化你的博客园,flash插件的使用
    Redis学习——详解Redis配置文件(三)
    Redis学习——Windows环境下Redis的安装(二)
    Redis学习——Linux环境下Redis的安装(一)
    NPM 安装速度慢,镜像修改
    日志配置(springboot、mybatis、Lombok)
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7403946.html
Copyright © 2011-2022 走看看