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

    题目大意:
    n个人,每个的都有一个值d,定义不开心值为(k-1)*d,k代表第k个出场,根据入栈出栈的不同情况,
    最后会有很多种出场顺序,求最小的总不开心值。
    可得情况数是个卡特兰数,第100项很大,而且不好枚举。
    dp[i][j]可通过枚举中间点得到。有两种状态:(pre为前缀和,sum[i][j]代表出场顺序为j~i的总不开心值)
    1.dp[i][k]+dp[k+1][j]+(pre[j]-pre[k])*(k-i+1)
    2.dp[k+1][j]+sum[i][k]+(pre[k]-pre[i-1])*(j-k)
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int INF=0x3f3f3f3f;
    int a[105],pre[105],dp[105][105],sum[105][105];
    int n;
    int main()
    {
        int T,o=0;
        cin>>T;
        while(T--)
        {
            memset(pre,0,sizeof pre);
            memset(sum,0,sizeof sum);
            memset(dp,INF,sizeof dp);
            cin>>n;
            for(int i=1;i<=n;i++)///预处理前缀和
                cin>>a[i],pre[i]=pre[i-1]+a[i],dp[i][i]=0;
            for(int j=1;j<=n;j++)///预处理倒着的不开心值
                for(int i=j-1;i>0;i--)
                    sum[i][j]=sum[i+1][j]+a[i]*(j-i);
            for(int j=1;j<=n;j++)
                for(int i=j-1;i>0;i--)
                    for(int k=i;k<j;k++)
                    {
                        dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+(pre[j]-pre[k])*(k-i+1));///先i~k,后k+1~j
                        dp[i][j]=min(dp[i][j],dp[k+1][j]+sum[i][k]+(pre[k]-pre[i-1])*(j-k));///先k+1~j,后i~k
                    }
            cout<<"Case #"<<++o<<": "<<dp[1][n]<<'
    ';
        }
    }
     
  • 相关阅读:
    梯度下降在实践I -特征缩放
    多变量的梯度下降
    多个变量的线性回归
    线性回归的梯度下降
    梯度下降的直觉
    梯度下降
    洛谷P1087--FBI树(二叉树)
    二叉树入门(洛谷P1305)
    二叉树--已知先序中序求后序--已知中序后序求先序(基本按照网上某大神思路搬过来的)
    多边形面积(计算几何)
  • 原文地址:https://www.cnblogs.com/zdragon1104/p/9171498.html
Copyright © 2011-2022 走看看