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

    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




    思路: 区间DP
    方程 f[i][j] 表示将i到j 搞完后的最小代价

    那么 要么i号人直接表演
    要么枚举i号人出小黑屋的顺序 f[i][j]=min(f[i][j],f[i+1][k]+f[k+1][j]+(k-i+1)*a[i]+(k-i)*(sum[j]-sum[k]));


    code:
    #include<bits/stdc++.h>
    using namespace std;
    #define maxnn 500
    int f[maxnn][maxnn];
    
    int a[maxnn];
    int t;
    int sum[maxnn];
    int tmp;
    int main()
    {
           
        cin>>t;int n,m;
        int aa,bb;
        while(t--)
        {
        tmp++;
            cin>>n;
             memset(f,0,sizeof(f));
            for (int i=1;i<=n;i++) f[i][i]=0;
            for (int i=1;i<=n;i++)
            for (int j=i+1;j<=n;j++)
              f[i][j]=1e9;
            for(int i=1;i<=n;i++)
            {
                cin>>a[i];
                sum[i]=sum[i-1]+a[i];
            }
            for(int i=n-1;i>=1;i--)
            for(int j=i+1;j<=n;j++)
            {
                for(int k=i;k<=j;k++)
                {
                    f[i][j]=min(f[i][j],f[i+1][k]+f[k+1][j]+a[i]*(k-i)+(k-i+1)*(sum[j]-sum[k]));
                }
            }
              printf("Case #%d: %d
    ",tmp,f[1][n]);
        }
    }
    刀剑映出了战士的心。而我的心,漆黑且残破
  • 相关阅读:
    git版本控制工具的使用(1)。
    python ui学习过程,使用pyqt5实现
    python下使用opencv拍照
    python的数字图像处理学习(3)
    python的数字图像处理学习(2)
    python的数字图像处理学习(1)
    tensorflow下识别手写数字基于MLP网络
    使用tensorflow下的GPU加速神经网络训练过程
    LSTM长短期记忆神经网络模型简介
    RNN模型(递归神经网络)简介
  • 原文地址:https://www.cnblogs.com/OIEREDSION/p/11277291.html
Copyright © 2011-2022 走看看