You Are the One

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6706    Accepted Submission(s): 3339

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
 
Source
 
Recommend
liuyiding
 
昨天入门区间dp感觉简单的一批,今天推转移方程都推的快睡着了,其实这题也是一道很模版的区间dp,我们走一遍思路,不感兴趣的可以跳过这一段。
 
要解决的问题是什么?
  给定一个数列,让你每次删除其中的一个数,删除这个数的代价为它和左右的乘积,最后要将数列中除了首尾的数字全都删完,问最小代价。
 
  这题一看,每次要删一个数,然后删除这个数的代价为它和左右两个数的乘积,一看就知道,数删除的顺序会影响最后的结果,但是我们不可能枚举每个删除数的顺序,所以我们可以发现,当一个数被删除之后,它左边的数在删除的时候会乘它右边的数,也就是说枚举任意三个数的乘法,只需要满足这三个数之间的所有数字都被删除即可,所以我们用dp[ i ][ j ]表示区间(i, j)的数字都被删除的最小代价。那么很容易可以看出是个区间dp了。
也就是一般套路,第一层枚举区间长度len,第二层枚举区间起点 i ,第三层枚举断点 k,我们可以知道我们枚举区间的终点 j 为i + len,所以就可以得到状态转移方程为dp[ i ][ j ] = min(dp[ i ][ k ] + dp[ k ][ j ] + val[ i ] * val[ k ] * val[ j ], dp[ i ][ j ])。
 
所以你知道如何判断一个题是否是区间dp了么?
 
 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + , inf = 0x3f3f3f3f; int n, value[maxn]; int dp[maxn][maxn]; int main() {
scanf("%d", &n);
for(int i = ; i <= n; i ++) {
scanf("%d", &value[i]);
}
for(int len = ; len < n; len ++) {
for(int i = ; i + len <= n; i ++) {
int j = i + len;
dp[i][j] = inf;
for(int k = i + ; k < j; k ++) {
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + value[i] * value[k] * value[j]);
}
}
}
printf("%d\n", dp[][n]);
return ;
}
 
 

multiplication_puzzle(区间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可以从这串数字的两端任意选数字,一次只能 ...

随机推荐

  1. [BZOJ5249][九省联考2018]IIIDX:线段树+贪心

    分析 GXZlegend orz 构造出一组合法的解并不是难事,但是我们需要输出的是字典序最大的解. 字典序最大有另一种理解方式,就是让越小的数尽量越靠后. 我们从树的根结点出发,从1开始填数,构造出 ...

  2. js几种加密方法

    1.base64加密 它的github地址:https://github.com/dankogai/js-base64 <!DOCTYPE HTML> <html> <h ...

  3. kafka监控指标项

    监控配置 ​ kafka基本分为broker.producer.consumer三个子项,每一项的启动都需要用到 $KAFKA_HOME/bin/kafka-run-class.sh 脚本,在该脚本中 ...

  4. [CSP-S模拟测试]:五子棋(模拟)

    题目传送门(内部题122) 输入格式 输入文件第一行为一个正整数$n$,表示双方总共下了多少步棋. 接下来$n$行,输入文件每行两个正整数.第$i$行的两个数$x,y$表示第$i$步的棋子下在了第$x ...

  5. Zookeeper入门(七)之Java连接Zookeeper

    Java操作Zookeeper很简单,但是前提要把包导对. 关于Zookeeper的Linux环境搭建可以参考我的这篇博客:Linux环境下Zookeeper安装 下面进入正题: 一.导入依赖 < ...

  6. spring boot 常用注解

    @RestController和@RequestMapping注解 4.0重要的一个新的改进是@RestController注解,它继承自@Controller注解.4.0之前的版本,spring M ...

  7. VS部分安全函数用法

    在 VS(Visual Studio)下编译C语言程序,有时候VS会报错,提示用到的一些函数可能不安全,并且建议替换为带有_s后缀的安全函数. 安全函数是什么 scanf().gets().fgets ...

  8. 从curl命令获取libcurl的用法

    libcurl的用法参数太多 有时候弄不好 可以先用curl命令实现了 然后获取相应的libcurl代码 比如要上传文件 curl -T d:/h.txt http://demo.xudp.cn/up ...

  9. 使用OMF特性

    Oracle 的OMF全称"Oracle managed file",关于这个概念的参考请自行查阅Oracle官方文档"Using Oracle-Managed File ...

  10. IDEA项目追踪快捷键

    1.查看某个方法在哪里被调用: 在方法上右键选择FindUsages: 快捷键,在方法上Ctrl+G 2.从Ctroller方法直接跳过接口找到实现类方法: 在方法上右键:选择GoTo>Impl ...