zoukankan      html  css  js  c++  java
  • 区间DP HDU 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


    思路:
    题目大意:现在给定一个序列,表示每个人的不开心值D,假如他是第x个上场,那他就有(x-1)*D的不开心值,因为他等了k-1个人,
    你有一个类似于栈的东西,可以使用它来改变整个序列的顺序,求最少的 不开心值的和QWQ由于修改顺序的时候,是要用类似栈的方式QWQ所以没法贪心.....
    这个题有一个性质,若第i个人是通过栈调整之后,第k个表演的 那么他能造成的影响 是可以比较容易计算的。我们令f[l][r]表示 l~r这些人的最小等待时间首先预处理一下所有人的等待时间的前缀和sum[i],
    便于计算对于一个区间l r 我们枚举第l个人是第几个出去的k从l~r假设他是第k个出去的则 f[l][r]=min(f[l][r],f[l+1][k]+f[k+1][r]+(k-l)*a[i]+(k-l+1)*(sum[r]-sum[k]))
    这里有几个要注意的地方:1>是f[l+1][k] 而不是 f[l][k]  因为这里枚举的就是第l个人的出栈顺序2>(k-l)表示在这个人之前有几个人出去 (k-1+1)表示算上这个人 出去了几个人


    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]);
        }
    }


    刀剑映出了战士的心。而我的心,漆黑且残破
  • 相关阅读:
    Windows-快速预览文件-QuickLook
    Chrome简洁高效管理下载项
    有Bug?你的代码神兽选对了吗
    保护视力-刻不容缓
    一次看懂 Https 证书认证
    Web前端助手-功能丰富的Chrome插件
    Chrome自动格式化Json输出
    网络爬虫
    彻底搞懂Cookie,Session,Token三者的区别
    Redis内存满了的解决办法
  • 原文地址:https://www.cnblogs.com/OIEREDSION/p/11273709.html
Copyright © 2011-2022 走看看