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. 转 多个版本的数据库在同一服务器上ORA-12557

    http://blog.chinaunix.net/uid-42518-id-3153473.html 问题描述:当同一台机子上安装了多个版本的数据库,可能在连接库或ASM时会报以下错误.ORA-12 ...

  2. 用 Fleck 实现 websocket 通信

    <html lang="en"> <head> <meta charset="utf-8"> <title>rf ...

  3. 利用layui的load模块解决图片上传

    首先肯定要参考layui官网的upload模块文档:http://www.layui.com/doc/modules/upload.html 讲讲思路:在一份添加表单中,我们有个图片上传的模块,然后我 ...

  4. servlet生命周期:

    Servlet生命周期分为三个阶段: 1,初始化阶段  servlet实例创建时调用init()方法,在Servlet的整个生命周期内,init()方法只被调用一次. 2,响应客户请求阶段 调用ser ...

  5. P1615 西游记公司

    题目背景 一道极其无厘头的题目 题目描述 事情是这样的:西游记中的孙沙猪(孙杀猪)三徒弟在西天取经之后开始进入厦门大学经贸系学习经济,在1个小时的学习后,他们用暴力手段毕业了.然后,他们创办了三个公司 ...

  6. vscode中将本地数据push至git repository

    1.新建repository 2.本地写好的代码 3.执行git init 初始化git配置文件 4.提交已暂存文件 5.填写提交信息 6.执行push命令 7.完成

  7. Python 版本对比

    python2 与 python3可认为代码不通用,你也可以点击Python2.x与3​​.x版本区别来查看两者的不同 python3.6以上支持f-string,一种很方便的变量替换方式 高版本可能 ...

  8. mysql利用binlog恢复数据

    需求:需要给开发提供一个2018年9月30号的数据,按照我们公司正常备份策略来说,直接找到对应时间的备份数据,解压导入即可,恰好这个时间节点的数据没有,只备份到2018年9月25号的,糟糕了吧 咋办呢 ...

  9. 一个Lucene.Net的Demo

    今天突然想来看一下全文检索,于是就了解了一下Lucene.Net,然后把公司目前的产品表拿来练手,写了这么个Demo. 先看一下Demo的代码 public class ProductReposito ...

  10. linux_2

    mac和linux上默认安装着SSH客户端,Windows上需要自己安装个软件. Windows下SSH客户端的安装:建议从官方网站下载正式程序安装(免费) Putty:https://www.chi ...