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. 题解报告:hdu 1392 Surround the Trees(凸包入门)

    Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround a ...

  2. C# HashSet 用法[转]

    原文链接 .NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>.这个集合类包含不重复项的无序列表.这种集合称为“集(se ...

  3. [已读]HTML5与CSS3设计模式

    我想说,不要被书名骗了,其实并没有涉及丁点h5与css3的内容,但是确实称得上比较详细的一本关于css的书.看它的页数就知道了,481~~ 今年上半年看完的,现在想想,觉得自己还是蛮拼的.内容会比较枯 ...

  4. MySQL与RAID

    RAID在mysq中适用场景 raid0:由于性能高和成本低,以及基本没有数据恢复的能力,而且它比单片磁盘损坏的概率要高.建议只在不担心数据丢失的情况下使用,如备库(slave)或者某些原因" ...

  5. zoj3772Calculate the Function(矩阵+线段树)

    链接 表达式类似于斐波那契 但是多了一个变量 不能用快速幂来解 不过可以用线段树进行维护 对于每一个点够一个2*2的矩阵 1 a[i] 1  0   这个矩阵应该不陌生 类似于构造斐波那契的那个数列 ...

  6. AJPFX简述Context.startService()和Context.bindService

    Context.startService()和Context.bindService 服务不能自己运行,需要通过调用Context.startService()或Context.bindService ...

  7. markdown快捷键(转)

    markdown快捷键 GLYPH NAME COMMAND Command, Cmd, Clover, (formerly) Apple ALT Option, Opt, (Windows) Alt ...

  8. Hello Shell

    shell是Linux平台的瑞士军刀,能够自动化完成很多工作.要了解UNIX 系统中可用的 Shell,可以使用 cat /etc/shells 命令.使用 chsh 命令 更改为所列出的任何 She ...

  9. java比较日期大小及日期与字符串的转换【SimpleDateFormat操作实例】

    java比较日期大小及日期与字符串的转换[SimpleDateFormat操作实例] package com.ywx.test; import java.text.ParseException; im ...

  10. Java编译时根据调用该方法的类或对象所属的类决定

    class Base{     int x = 1;     static int y = 2; } class Subclass extends Base{     int x = 4;     i ...