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  

原文地址:https://www.cnblogs.com/1013star/p/10412084.html

时间: 2024-11-10 00:06:56

You Are the One(区间DP 好题)的相关文章

合并石子 区间dp水题

合并石子 链接: nyoj 737 描述: 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆石子堆成一堆,每次合并花费的代价为这两堆石子的和,经过N-1次合并后成为一堆.求出总的代价最小值. tags:最基本的区间dp,这题范围小,如果n大一些,还是要加个平行四边行优化. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring&g

石子合并 区间DP模板题

题目链接:https://vjudge.net/problem/51Nod-1021 题意 N堆石子摆成一条线.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合并成新的一堆,并将新的一堆石子数记为该次合并的代价.计算将N堆石子合并成一堆的最小代价. 例如:1 2 3 4 ,有不少合并方法 1 2 3 4 => 3 3 4(3) => 6 4(9) => 10(19) 1 2 3 4 => 1 5 4(5) => 1 9(14) => 10(24) 1 2 3

LightOJ 1422 Halloween Costumes (区间dp 好题)

1422 - Halloween Costumes PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Gappu has a very busy weekend ahead of him. Because, nextweekend is Halloween, and he is planning to attend as many parties as he can.Since it's Hall

HDU 4283:You Are the One 区间DP好题

String painter 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4283 题意: 有n个人参加选秀,每个人有一个Di值,Di值*(出场时间-1)决定他们的悲惨度,这i个人按顺序出场,但是在出场前可以通过一个栈略微调整出场顺序,输出调整后的总悲惨度最小值. 题解: 设dp[i][j]是区间[i,j]部分的最小悲惨值和,终点状态为dp[1][n].可以发现,对于每个[i,j]区间不需要考虑 i 以前和 j 以后的部分,那么 i 就是第一个

POJ 3280:Cheapest Palindrome 区间DP好题

Cheapest Palindrome 题目链接: http://poj.org/problem?id=3280 题意: 给出一个只由小写字母组成的串,可以添加或删除一些字母(添加和删除都需要花费且花费不同),求将这个串改变成一个回文串的最小花费. 题解: 设dp[i][j]是将区间[i,j]改变成回文串的最小花费,则两种情况 ①显而易见,当s[i]==s[j]时,dp[i][j]=dp[i+1][j-1] ②当s[i]!=s[j]时,dp[i][j]有四种可能的取值,区间 [i,j-1] 删除

Light OJ 1422 Halloween Costumes 区间DP基础题

Halloween Costumes 题目链接: http://lightoj.com/volume_showproblem.php?problem=1422 题意: Gappu想要去参加一些party,他去每个party都要把特定编号的服装穿在外边,他可以穿上或者脱掉服装(脱掉的服装不能再穿一次,但是可以穿一件相同编号新的服装,最近穿的服装会套在之前穿的服装的外边),问Gappu最少需要准备多少套服装. 题解: 设dp[i][j]为区间 i 到 j (设len为区间长度,j=i+len)内最少

POJ 2955:Brackets 区间DP基础题

Brackets 题目链接: http://poj.org/problem?id=2955 题意: 给出一个只由'('.')'.'['.']'构成的字符串,字符间可以匹配,左边的 '(' 可以与右边的 ')' 匹配,左边的 '[' 可以与右边的 ']' 匹配 两种匹配不能交叉,可以包含,如 [(])只算2个匹配而[()]算四个匹配,求最大匹配数. 题解: 设dp[i][j]为区间 i 到 j (设len为区间长度,j=i+len)内可以得到的最大匹配数,则有两种情况: ① j 不与区间[i,j]

区间DP [NYOJ 536] 开心的mdd

开心的mdd 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 himdd有一天闲着无聊,随手拿了一本书,随手翻到一页,上面描述了一个神奇的问题,貌似是一个和矩阵有关的东西. 给出三个矩阵和其行列A1(10*100),A2(100*5),A3(5*50).现在himdd要算出计算矩阵所要的乘法次数,他发现不同的计算次序,所要的乘法次数也不一样, 如: (A1*A2)*A3 : 10*100*5+5*10*50=7500; A1*(A2*A3) : 5*100*50+1

自己看之区间DP

//菜鸡制作,看的时候可能三目运算符略烦;;; 区间DP入门题:Brackets 地址:http://59.77.139.92/Problem.jsp?pid=1463 分析(对区间DP的代码原理进行分步解析): 1 for(k=1; k<L; k++) 2 { 3 for(i=0, j=k; j<L; i++, j++) 4 { 5 if(s[i]=='['&&s[j]==']'||s[i]=='('&&s[j]==')') 6 dp[i][j]=dp[i+1