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. JS实现表格隔行变色

    用到的鼠标事件:(1)鼠标经过 onmouseover:(2)鼠标离开 onmouseout 核心思路:鼠标经过 tr 行的时候,当前行会改变背景颜色,鼠标离开的时候去掉背景颜色. 注意:第一行(th ...

  2. Linux任务计划及周期性任务执行:at、crontab命令

    一.概述 未来的某时间点执行一次某任务:at, batch 周期性运行某任务:crontab 这两个任务的执行结果:会通过邮件发送给用户 (本地终端用户之间的邮件通知) centos 5,6,7默认开 ...

  3. BZOJ 2669 Luogu P3160 [CQOI2012]局部极小值 (容斥原理、DP)

    题目链接 (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=2669 (luogu) https://www.luogu.org/prob ...

  4. SpringMVC参数传递 HttpServletRequest,HttpServletResponse和HttpSession

    SpringMVC参数传递 HttpServletRequest,HttpServletResponse和HttpSession 2017-11-27 16:44:51 douunderstand 阅 ...

  5. ajax异步加载查询数据库

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. JS利用XMLHttpRequest拦截ajax请求

    function XMLHttpRequestBreak(fun=()=>false){ let f = XMLHttpRequest.prototype.open; let add = fun ...

  7. 作业要求20191010-4 alpha week 1/2 Scrum立会报告+燃尽图 02

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/8747 一.小组情况组长:贺敬文组员:彭思雨 王志文 位军营 杨萍队名:胜 ...

  8. 高并发架构系列:Redis为什么是单线程、及高并发快的3大原因详解

    Redis的高并发和快速原因 1.redis是基于内存的,内存的读写速度非常快: 2.redis是单线程的,省去了很多上下文切换线程的时间: 3.redis使用多路复用技术,可以处理并发的连接.非阻塞 ...

  9. 让socket端口不被子进程继承

    有两个程序A,B A绑定端口8000,然后用system函数启动程序B,然后再把A杀掉,这个时候再重启A的时候会发现绑定端口失败, 原因是端口被B绑定了,实际上是被继承了,为了避免这种情况,需要对主s ...

  10. 使用tensorflow.data.Dataset构造batch数据集(具体用法在下一篇博客介绍)

    import tensorflow as tf import numpy as np def _parse_function(x): num_list = np.arange(10) return n ...