zoukankan      html  css  js  c++  java
  • You Are the One(区间DP 好题)

    You Are the One

     HDU - 4283 

     

     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

    题意:

    一群屌丝要上台表演节目, 由于舞台太小,每次只允许一个人表演,所以屌丝们在后台排起了长长的队,每位屌丝还有个屌丝值ai,轮不到他他就难受,不高兴,如果前边有k个人,他的怒气值就为ai*k;后台呢,还有个小黑屋,可以临时把一个屌丝值小的屌丝关进去,让后边的人先上台,小黑屋里可以关任意数量的人,但是先进去的要最后出来;问通过小黑屋安排后怒气和的最小值。

    题解:

      设现在区间[l,r]的人要上台表演,如果l位置的人第k个上场,即他前面会有[l+1,l+k-1]个人先他上场,那么第l位置的人的怒气值为a[l]*(k-1).而[l+1,l+k-1]区间的最优解为dp[l+1][l+k-1],第l位置后面的人有[l+k,r]每个人的怒气值会增加a[i]*k即总共增加了k*(sum[r]-sum[l+k-1]) 故当L第K个上台的时候,dp[L][R]=dp[L+1][L+K-1]+D[L]*k+dp[L+K][R]+K*(sum[R]-sum[L+K-1]); k的范围是1~r-l+1。

     1 import java.util.Scanner;
     2 
     3 public class Main { 
     4     static int casen,n,inf = 10000000;
     5     static int [][] dp ;
     6     static int [] a = new int [110];
     7     static int [] sum = new int [110];
     8     public static void main(String[] args) {
     9         Scanner cin = new Scanner(System.in);
    10         casen = cin.nextInt();
    11         int ca = 1;
    12         while(casen-->0) {
    13             n = cin.nextInt();
    14             dp = new int [n+5][n+5];
    15             for(int i=0;i<n+2;i++)
    16                 for(int j=0;j<n+2;j++)
    17                     dp[i][j] = 0;
    18             for(int i=1;i<=n;i++) {
    19                 a[i] = cin.nextInt();
    20                 sum[i] = sum[i-1]+a[i];
    21             }
    22             for(int len=2;len<=n;len++) {
    23                 for(int l=0;l+len-1<=n;l++) {
    24                     int r=l+len-1;
    25                     int tmp = inf;
    26                     for(int k=1;k<=len;k++) {
    27                         tmp = Math.min(tmp, dp[l+1][l+k-1]+a[l]*(k-1)+dp[l+k][r]+k*(sum[r]-sum[l+k-1]));
    28                     }
    29                     dp[l][r] = tmp;
    30                 }
    31             }
    32             System.out.println("Case #"+ ca++ +": "+dp[1][n]);
    33         }
    34     }
    35 }
    36  
  • 相关阅读:
    2019年春阅读笔记13——分布式系统的两种方式
    2019年春阅读笔记12——索引数据结构设相关的计算机原理
    2019年春阅读笔记11——常见的查询算法及数据结构
    2019年春阅读笔记10——数据库优化
    2019年春阅读笔记9——一些基础优化
    2019年春阅读笔记8——从不同层面进行SQL优化
    2019年春阅读笔记7——关于SQL优化
    2019年春阅读笔记6——继续说开源
    jQuery使用小技巧
    marquee 实现首尾相连循环滚动效果
  • 原文地址:https://www.cnblogs.com/1013star/p/10412084.html
Copyright © 2011-2022 走看看