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;
        }
    }
  • 相关阅读:
    bzoj 3027 [Ceoi2004]Sweet——生成函数
    bzoj 3028 食物——生成函数
    JZOJ 5461 购物 —— 贪心
    JZOJ 1003 [ 东莞市选 2007 ] 拦截导弹 —— 递推
    JZOJ 1667 ( bzoj 1801 ) [ AHOI 2009 ] 中国象棋 —— DP
    洛谷 P2055 [ ZJOI 2009 ] 假期的宿舍 —— 二分图匹配
    洛谷 P3398 仓鼠找sugar —— 树链剖分
    洛谷 P1083 [ NOIP 2012 ] 借教室 —— 线段树 / 二分差分数组
    bzoj 3895 取石子 —— 博弈论
    洛谷 P1312 [ NOIP 2011 ] Mayan游戏 —— 搜索+模拟
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7403946.html
Copyright © 2011-2022 走看看