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\n",t,dp[1][n]);
36     }
37     return 0;
38 }

hdu4283(区间dp),布布扣,bubuko.com

时间: 2024-10-13 10:07:19

hdu4283(区间dp)的相关文章

hdu4283 区间dp

1 //Accepted 300 KB 0 ms 2 //区间dp 3 //dp[i][j] 表示i到j第一个出场的最小diaosizhi 4 //对于i到j考虑元素i 5 //(1)i第一个出场,diaosizhi为 dp[i+1][j]+sum(i+1--j) 6 //(2)i不是第一个出场,而是第k个出场,则i+1到k+i-1这段区间第一个出场,k+i到j第k+1个出场 7 //diaoshizhi为dp[i+1][i+k-1] + a[i]*(k-1) + (dp[i+k][j]+k*s

区间DP基础篇之 HDU4283——You Are the One(非诚勿扰)

题目大意: 有n个男屌丝事先按1,2,3,,,,,,n的顺序排好,每个人都有一个不开心值unhappy[i],如果第i个人第k个上台找对象,那么该屌丝男的不开心值就会为(k-1)*unhappy[i],因为在他前面有k-1个人嘛,导演为了让所有男屌的总不开心值最小,搞了一个小黑屋,可以通过小黑屋来改变男屌的出场顺序 注意:这个小黑屋是个栈,男屌的顺序是排好了的,但是可以通过入栈出栈来改变男屌的出场顺序 解题思路:(操度娘所知~度娘你好腻害) dp[i][j]表示区间[i,j]的最小总不开心值 把

HDU-4283 You Are the One (区间DP)

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 ar

HDU4283:You Are the One(区间DP)

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 ar

hdu4283---You Are the One(区间dp)

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 ar

uva 10003 Cutting Sticks 简单区间dp

// uva 10003 Cutting Sticks 区间dp // 经典的区间dp // dp(i,j)表示切割小木棍i-j所需要的最小花费 // 则状态转移为dp(i,j) = min{dp(i,k) + dp(k,j) + a[j]-a[i]) // 其中k>i && k<j // a[j] - a[i] 为第一刀切割的代价 // a[0] = 0,a[n+1] = L; // dp数组初始化的时候dp[i][i+1]的值为 0,这表示 // 每一段都已经是切割了的,不

黑书例题 Fight Club 区间DP

题目可以在bnuoj.soj等OJ上找到. 题意: 不超过40个人站成一圈,只能和两边的人对战.给出任意两人对战的输赢,对于每一个人,输出是否可能是最后的胜者. 分析: 首先序列扩展成2倍,破环成链. dp[i][j]表示i和j能够相遇对打,那么dp[i][i+n]为真代表可以成为最后胜者. 枚举中间的k,若i和j都能和k相遇,且i和j至少一人能打赢k,那么i和j可以相遇. 复杂度o(n^3) 1 #include<cstdio> 2 #include<cstring> 3 usi

算法复习——区间dp

感觉对区间dp也不好说些什么直接照搬讲义了2333 例题: 1.引水入城(洛谷1514) 这道题先开始看不出来到底和区间dp有什么卵关系···· 首先肯定是bfs暴力判一判可以覆盖到哪些城市····无解直接输出···有解得话就要想想了···· 这道题关键是要发现··如果一个蓄水池所在城市可以覆盖到一些沙漠城市···那么这些沙漠城市肯定是一段····不然假设有一个城市是断开的而两边都被同一个蓄水池流出的水覆盖,这个城市四周的城市都肯定比它矮···(不理解举个反例吧···反正我举不出来)···然后就

合并石子 区间dp水题

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