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;
        }
    }
  • 相关阅读:
    Hibernate注解(一对一、一对多、多对多)
    Hibernate多表关系配置——继承
    Hibernate多表关系配置——多对多对关系映射
    Hibernate多表关系配置——一对一关系映射
    Hibernate多表关系配置——多对一关系映射
    初识Hibernate——添加数据
    Servlet学习总结
    jQuery动态添加Table行
    VS2013搭建CSDN源代码管理git
    Node.js amqplib 连接 Rabbitmq 学习笔记
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7403946.html
Copyright © 2011-2022 走看看