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 题目大意:
n个人,每个的都有一个值d,定义不开心值为(k-1)*d,k代表第k个出场,根据入栈出栈的不同情况,
最后会有很多种出场顺序,求最小的总不开心值。
可得情况数是个卡特兰数,第100项很大,而且不好枚举。
dp[i][j]可通过枚举中间点得到。有两种状态:(pre为前缀和,sum[i][j]代表出场顺序为j~i的总不开心值)
1.dp[i][k]+dp[k+1][j]+(pre[j]-pre[k])*(k-i+1)
2.dp[k+1][j]+sum[i][k]+(pre[k]-pre[i-1])*(j-k)
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int INF=0x3f3f3f3f;
int a[],pre[],dp[][],sum[][];
int n;
int main()
{
int T,o=;
cin>>T;
while(T--)
{
memset(pre,,sizeof pre);
memset(sum,,sizeof sum);
memset(dp,INF,sizeof dp);
cin>>n;
for(int i=;i<=n;i++)///预处理前缀和
cin>>a[i],pre[i]=pre[i-]+a[i],dp[i][i]=;
for(int j=;j<=n;j++)///预处理倒着的不开心值
for(int i=j-;i>;i--)
sum[i][j]=sum[i+][j]+a[i]*(j-i);
for(int j=;j<=n;j++)
for(int i=j-;i>;i--)
for(int k=i;k<j;k++)
{
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+][j]+(pre[j]-pre[k])*(k-i+));///先i~k,后k+1~j
dp[i][j]=min(dp[i][j],dp[k+][j]+sum[i][k]+(pre[k]-pre[i-])*(j-k));///先k+1~j,后i~k
}
cout<<"Case #"<<++o<<": "<<dp[][n]<<'\n';
}
}
 

You Are the One (区间DP)的更多相关文章

  1. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  2. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  3. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  4. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  5. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

  6. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  7. BZOJ 1260&UVa 4394 区间DP

    题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...

  8. 区间dp总结篇

    前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...

  9. Uva 10891 经典博弈区间DP

    经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...

  10. 2016 年沈阳网络赛---QSC and Master(区间DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5900 Problem Description Every school has some legend ...

随机推荐

  1. Zernike矩之图像重建(附源码)

    源码下载 参考: [1] Teague M R. Image analysis via the general theory of moments[J]. JOSA, 1980, 70(8): 920 ...

  2. Log4net系列二:Log4net邮件日志以及授权码

    Log4net邮件发送 上篇文章我们主要介绍Log4net生成文本格式,本篇文章主要配置邮箱发送.关于项目的引用,搭建我们就不在描述,如果不太清楚,请看上篇文章, 老规矩,我们现在配置文件中添加一个a ...

  3. 使用grunt构建前端项目

    1. grunt构建工具是基于nodejs上的,所以在使用之前一定要先安装好nodejs 2. 安装好nodejs后,node -v查看node版本 npm-v 查看npm版本信息 3. 在需要用到的 ...

  4. 雪碧图(background-position)、overflow、title中的小图标、光标、rgb 和opacity 与rgba

    一.background-position     雪碧图 我们的html和css中有三个属性可以向服务器发送请求:src   url    href 1.我们为什么使用雪碧图? 因为我们使用雪碧图之 ...

  5. -bash: mysql: command not found 之 MAC

    第一次尝试: ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql 提示:Operation not permitted 再次,加sudo附上管理员权限,依旧 ...

  6. [Java 8] (9) Lambda表达式对递归的优化(下) - 使用备忘录模式(Memoization Pattern) .

    使用备忘录模式(Memoization Pattern)提高性能 这个模式说白了,就是将需要进行大量计算的结果缓存起来,然后在下次需要的时候直接取得就好了.因此,底层只需要使用一个Map就够了. 但是 ...

  7. (转)@Autowire注解与自动装配

    http://blog.csdn.net/yerenyuan_pku/article/details/52860713 前面我们已经学会使用@Resource注解注入属性,并且我们还编码剖析了@Res ...

  8. RGB颜色空间、色调、饱和度、亮度,HSV颜色空间详解

    本文章会详细的介绍RGB颜色空间与RGB三色中色调.饱和度.亮度之间的关系,最后会介绍HSV颜色空间! RGB颜色空间 概述 RGB颜色空间以R(Red:红).G(Green:绿).B(Blue:蓝) ...

  9. select2宽度占比100%,导致无法实现浮动效果

  10. ubuntu命令行转换图片像素大小

    convert -resize 512x256 00433.png 00001.png 1.512和256之间是x(就是字母那个x),用' * '反而会报错 2.这个命令会按照原图的比例进行转换 3. ...