zoukankan      html  css  js  c++  java
  • 刷题总结——you are the one(hdu4283)

    题目:

    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到j中的k,考虑让第i人再第k个人之后上场··那么第i个人就是第i-k+1个上场的··对答案贡献是(i-k)*num[i],另外k+1到j个人也会对答案有新的贡献,为(i-k+1)*sum[k+1——j]

    代码 

      

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<ctime>
    #include<cctype>
    #include<string>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=105;
    const int inf=0x3f3f3f3f;
    int num[N],sum[N],f[N][N],T,n;
    inline int R()
    {
      char c;int f=0;
      for(c=getchar();c<'0'||c>'9';c=getchar());
      for(;c<='9'&&c>='0';c=getchar())  f=(f<<3)+(f<<1)+c-'0';
      return f; 
    }
    int main()
    {
     // freopen("a.in","r",stdin);
      T=R();
      for(int t=1;t<=T;t++)
      {
        n=R();
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=n;i++)  num[i]=R(),sum[i]=sum[i-1]+num[i];
        memset(f,0,sizeof(f));
        for(int i=1;i<=n;i++)
          for(int j=i+1;j<=n;j++)  f[i][j]=inf;
        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]+(k-i+1)*(sum[j]-sum[k])+num[i]*(k-i));
        printf("Case #%d: %d
    ",t,f[1][n]);
      }
      return 0;
    }
  • 相关阅读:
    判断JS数据类型的四种方法
    JavaScript正则表达式精简
    virtio介绍
    DPDK与SRIOV应用场景及性能对比
    KVM和Xen的区别
    理解 JavaScript 闭包
    JS数组常用操作方法总结
    JavaScript中的 NaN 与 isNaN
    如何在Unity中复制多个组件并粘贴到另一个GameObject上
    Unity Umotion 导入动作发生漂移的解决办法
  • 原文地址:https://www.cnblogs.com/AseanA/p/7703618.html
Copyright © 2011-2022 走看看