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;
        }
    }
  • 相关阅读:
    oracle 物化视图导入导出报错
    算法导论------------桶排序算法之研究
    实现一个做双向NAT的虚拟网卡
    合理的keyword密度散布与黑帽SEO之躲藏文本
    Java发送Email
    ubuntu系统下设置静态IP
    Design Pattern Iterator 迭代器设计模式
    概率论高速学习03:概率公理补充
    鄂尔多斯
    衬衫面料品牌:Alumo_衬衫_男装_男装:衬衫、法式衬衫、袖扣领带、西服西裤等男士正装服饰-仕族官网
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7403946.html
Copyright © 2011-2022 走看看