zoukankan      html  css  js  c++  java
  • hdu4283(区间dp)

    You Are the One

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1585    Accepted Submission(s): 756


    Problem Description
      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,dp[i][j]表示第i个人到第j个人这个区间的最小花费(只考虑j-i+1个人,不需要考虑在它前面有多少人)
    对于dp[i][j]的第i个人可能第一个上场,也有可能第j-i+1个上场,考虑其第k个上场,那么i+1之后的k-1个人首先上场,那么就出现了一个子问题 dp[i+1][i+1+k-1-1]表示在第i个人之前上场的
    对于第i个人,由于是第k个上场的,那么愤怒值便是a[i]*(k-1)
    其余的人是排在第k+1个之后出场的,也就是一个子问题dp[i+k][j],对于这个区间的人,由于排在第k+1个之后,所以整体愤怒值要加上k*(sigma(i+k--j))
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 using namespace std;
     6 const int maxn = 105;
     7 const int oo = 99999999;
     8 int dp[maxn][maxn];
     9 int a[maxn],sum[maxn];
    10 int main()
    11 {
    12     int T,t,n;
    13     scanf("%d",&T);
    14     for (t = 1; t<=T; t++)
    15     {
    16         scanf("%d",&n);
    17         for (int i=1; i<=n; i++)
    18             scanf("%d",&a[i]);
    19         sum[0] = 0;
    20         for (int i=1; i<=n; i++)
    21             sum[i]=sum[i-1]+a[i];
    22         memset(dp,0,sizeof(dp));
    23         for (int i=1; i<=n; i++)
    24             for (int j=i+1; j<=n; j++)
    25                 dp[i][j]=oo;
    26         for (int len=1; len<n; len++)
    27         {
    28             for (int i=1; i<=n-len; i++)
    29             {
    30                 int j = i + len;
    31                 for (int k=1; k<=j-i+1; k++)
    32                     dp[i][j]=min(dp[i][j],dp[i+1][i+k-1]+dp[i+k][j]+(k-1)*a[i]+k*(sum[j]-sum[i+k-1]));
    33             }
    34         }
    35         printf("Case #%d: %d
    ",t,dp[1][n]);
    36     }
    37     return 0;
    38 }
    View Code
  • 相关阅读:
    升级xcode7.0 第三方库不能用的解决方法(bitcode是什么鬼?)
    java调用C#的dll
    微信JS SDK Demo
    【jQuery】smartMenu右键自定义上下文菜单插件(似web QQ)
    搭建Windows SVN服务器及TortoiseSVN使用帮助和下载
    微信jsSDK开发
    用c#开发微信(10) JSSDK 基本用法 分享接口“发送到朋友”
    MVC4.0网站发布和部署到IIS7.0上的方法
    C#的事件处理机制
    ASP.Net MVC开发基础学习笔记(1):走向MVC模式
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3890804.html
Copyright © 2011-2022 走看看