E - Multiplication Puzzle  POJ - 1651 这个题目没有特别简单,但是也没有我想象之中的那么难,这个题目时区间dp,因为我们是要对区间进行考虑的. 但是呢,这个也和动态规划的基本原理息息相关. 动态规划 我们一般需要去找这个的子问题,这个题目因为左端点和右端点是不能动 的,所以最后一个取走的就是a[l]*a[k]*a[r] 所以说这个题目的子问题就是每次去找一个区间,然后记录区间的左右端点,然后再找这个区间乘法最小和. dp[i][k]=min(dp[i][k],…
解法 区间dp例题,长度从2开始到n结束起点从1到n,中间枚举的时候是看着左端点右端点与中点的乘积 代码 #include <iostream> #include <cstring> using namespace std; int dp[666][666],num[666]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; memset(dp,0x7f,size…
卡片游戏 题目大意:给你一排卡片,你可以从从中抽一些卡片(但是不能抽最左和最右的卡片),每张卡片上有一个数字,当你从中抽出一张卡片后,你将得卡片的数字和卡片左右两张卡片的数字的乘积的分数,问当剩下最左和最右两张卡片的时候,你可以得到的最小的分数? 这一题一看好像挺复杂的,但是如果我们换一个思维方式,这一题就会变得很熟悉 首先这一题是显而易见的DP(找最小值,不能取极限方式) 首先他要我们抽卡片是吧,一开始我们可能会想到先抽一张看看,然后在扫一遍其他卡片看能否抽,但是这样带来的直接后果就是,我们很…
传送门:http://poj.org/problem?id=1651 Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13109   Accepted: 8034 Description The multiplication puzzle is played with a row of cards, each containing a single positive intege…
题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the prod…
Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken…
Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10010   Accepted: 6188 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one…
http://poj.org/problem?id=1651 题意:给出n个数字,每取中间一个数,就会使得权值加上中间这个数和两边的乘积,求取剩两个数最少的权值是多少. 思路:区间dp. 一开始想了挺久还是写不出方程,做了点别的事回来再想就突然觉得很简单了. 一开始使得长度为1和2的区间dp[i][j]为0. 然后dp[i][j] = min(dp[i][k] + dp[k][j] + w[k] * w[i] * w[j]). 枚举的k为中间拿掉的数,然后和左右的区间和相加就是最后答案了. #i…
题意: 给出一个序列,共n个正整数,要求将区间[2,n-1]全部删去,只剩下a[1]和a[n],也就是一共需要删除n-2个数字,但是每次只能删除一个数字,且会获得该数字与其旁边两个数字的积的分数,问最少可以获得多少分数? 思路: 类似于矩阵连乘的问题,用区间DP来做. 假设已知区间[i,k-1]和[k+1,j]各自完成删除所获得的最少分数,那么a[k]是区间a[i,j]内唯一剩下的一个数,那么删除该数字就会获得a[k]*a[i-1]*a[i+1]的分数了.在枚举k的时候要保证[i,j]的任一子区…
Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7118   Accepted: 4385 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one…
题目链接: http://poj.org/problem?id=1651 题目大意:加分取牌.如果一张牌左右有牌则可以取出,分数为左牌*中牌*右牌.这样最后肯定还剩2张牌.求一个取牌顺序,使得加分最少. 解题思路: 矩阵链乘的变种题. 假设有10.20.30.40.50五张牌. 如果我想要最后取30,则应该先取20.40,这样就还剩10.30.50三张牌了. 不难发现取20是dp[i][k]部分,取40是dp[k][j]部分,最后剩下的就是i.k.j三张牌. DP边界:无 因为是算加分,区间间隔…
Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken…
题目链接:https://vjudge.net/problem/POJ-1651 Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11239   Accepted: 6980 Description The multiplication puzzle is played with a row of cards, each containing a single positive…
Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9419 Accepted: 5850 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card…
题意:n个数字的串,每取出一个数字的代价为该数字和左右的乘积(1.n不能取),问最小代价 思路:dp[i][j]表示把i~j取到只剩 i.j 的最小代价. 代码: #include<set> #include<map> #include<cmath> #include<queue> #include<cstdio> #include<cstring> #include<algorithm> typedef long lon…
The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken and the numb…
题目链接:http://poj.org/problem?id=1179 Time Limit: 1000MS Memory Limit: 10000K Description Polygon is a game for one player that starts on a polygon with N vertices, like the one in Figure 1, where N=4. Each vertex is labelled with an integer and each e…
题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思路:比较简单的区间DP,令dp[i][j]表示使[i,j]回文的最小花费.则得到状态转移方程: dp[i][j]=min(dp[i][j],min(add[str[i]-'a'],del[str[i]-'a'])+dp[i+1][j]); dp[i][j]=min(dp[i][j],min(add[…
题目链接:http://poj.org/problem?id=3186 题目大意:给出的一系列的数字,可以看成一个双向队列,每次只能从队首或者队尾出队,第n个出队就拿这个数乘以n,最后将和加起来,求最大和. 解题思路:有两种写法: ①这是我一开始想的,从外推到内,设立数组dp[i][j]表示剩下i~j时的最优解,则有状态转移方程: dp[i][j]=dp[i][j]=max(dp[i-1][j]+a[i-1]*(n-(j-i+1)),dp[i][j+1]+a[j+1]*(n-(j+1-i)))…
Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29520   Accepted: 8406   Special Judge Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a re…
链接http://poj.org/problem?id=1160 很好的一个题,涉及到了以前老师说过的一个题目,可惜没往那上面想. 题意,给出N个城镇的地址,他们在一条直线上,现在要选择P个城镇建立邮局,使得每个城镇到离他最近的邮局距离的总和尽量小. 首先提一个这个问题的简化版本,如果P=1得话,这个距离是多少呢? 这个问题的解就是将这个唯一的邮局建在(l+r)/2的位置,答案就是最优解, 这个类似于中位数的概念,我们有一个数学归纳法简单的证明 数轴上有n个点,求到这n个点距离最小的一个点   …
dp题: 1.写状态转移方程; 2.考虑初始化边界,有意义的赋定值.还没计算的赋边界值: 3.怎么写代码自底向上计算最优值 今天做了几个基础dp,所有是dp方程写对可是初始化以及计算写错 先是poj 1651 事实上就是个赤裸裸的矩阵连乘.dp方程非常easy写出 dp[i][j]=min(dp[i][k]+dp[k+1][j]+r[i]*c[k]*c[j],dp[i][j]); 先贴两个个二逼的代码,mark下自己多么的二逼: 二逼一:在计算的时候使用了还没有算出来的值,模拟下就知道第一重循环…
由于,这两题有着似乎一样的解法所以将其放在一起总结比較,以达到更好的区分二者的差别所在. 一.区间DP uva的Cutting Sticks是一道典型的模板题. 题目描写叙述: 有一根长度为l的木棍,木棍上面有m个分割点,每一次分割都要付出当前木棍长度的代价,问如何分割有最小代价. 区间DP的定义: 区间动态规划问题一般都是考虑.对于每段区间,他们的最优值都是由几段更小区间的最优值得到,是分治思想的一种应用.将一个区间问题不断划分为更小的区间直至一个元素组成的区间,枚举他们的组合,求合并后的最优…
POJ - 3280 Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u id=16272" class="login ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="display:inline-block;…
ZOJ 3541 题目大意:有n个按钮,第i个按钮在按下ti 时间后回自动弹起,每个开关的位置是di,问什么策略按开关可以使所有的开关同时处于按下状态 Description There is one last gate between the hero and the dragon. But opening the gate isn't an easy task. There were n buttons list in a straight line in front of the gate…
http://poj.org/problem?id=2955 题意:给出一串字符,求括号匹配的数最多是多少. 思路:区间DP. 对于每个枚举的区间边界,如果两边可以配对成括号,那么dp[i][j] = dp[i+1][j-1] + 2,表示由上一个状态加上当前的贡献. 然后和普通的区间合并一样去更新. #include <cstring> #include <cstdio> #include <iostream> #include <string> usin…
POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间DP,以为就是用栈进行模拟呢,可是发现就是不大对,后来想到是不是使用DP,但是开始的时候自己没有推出递推关系,后来实在想不出来看的题解,才知道是区间DP,仔细一想确实是啊. 下面就是状态转移方程: \[ \begin{cases}dp[i][j] &=& dp[i+1][j-1]+if(str…
Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8737   Accepted: 5468 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one…
Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6033   Accepted: 3220 Description We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular…
比较好做的区间DP 状态转移方程:DP[i][j] 表示区间[i,j]最小的乘积和. DP[i][j] = MIN{DP[i][k-1]+DP[k+1][j] + a[k]*a[i-1]*a[j+1] | i<k<j}; 最后结果就是DP[2][N-1] #include <map> #include <set> #include <stack> #include <queue> #include <cmath> #include &…