这周搜了很多区间dp的资料,虽然大体上思路并不难,但是总觉得对于细节问题总感觉差点意思,所以我又搜了好几道区间dp的题目,又在纸上一步一步的划拉,总算对区间dp有了点是自己东西的感觉,所以千万不要纸上谈兵总要多刷点题目来更好的理解这种算法,好了话不多说,言归正传聊一下区间dp
所谓区间dp就是用一个二维数组模拟了这个求解最优值的过程,好比dp[i][j]就代表从i到j分别代表起始位置和终止位置,由他们所构成的区间的最优值,我查阅了好多种资料,发现区间dp的伪代码模板有好多种,有逆推的有递推的下面附上一种我觉得最好理解的模板
memset(dp, 0x3f, sizeof(dp));//dp数组清零处理
for (int i = 1; i <= n; i++) //区间长度为1的初始化
dp[i][i] = 0;
for (int len = 2; len <= n; len++) //枚举区间长度
{
for (int i = 1, j = len; j <= n; i++, j++) //区间[i,j]
{
//DP方程实现balabalabnal……
}
}
一般而言区间的状态转移方程是类似的,所有区间dp问题都可以根据基础的状态转移方程稍加修改就可以满足题意
先对当前区间进行初始化,dp[i][j]=dp[i+1][j]+1(加几或者不加根据题目要求来)代表的意义就是在i到j这个区间里面,如果i这个元素,也就是这个区间第一个元素符合题目要求,那么我接下来只需要计算从i+1到j这个区间的最优解即可,然后第二部就是如果在i到j这个区间里面有可能存在第k个元素也符合题目要求,那么就设循环来枚举,来看看有没有这样的k,这时状态转移方程就变成了
dp[i][j] = max(dp[i][j], dp[i+1][k-1] + dp[k+1][j] + 2);稍微解释一下这个加几都要看题目要求,这些都不一定,代表的意义就是i如果第k个元素如果匹配了第i个元素,那么我们就将题目要求的最优解分成了两个部分,只需要求从i+1到k-1这个区间还有k+1到j这个区间的最优解的和即可
虽然感觉自己已经挺明白的了,但是真的要细扣每个细节这样把这个过程掰开了揉碎了去讲解的话,讲了半天还是感觉乱七八糟的,附上几个简单区间dp的题目帮助理解一下吧
要说最经典的区间dp就是石子问题
题意:有n堆石子,每堆有a[i]个,每次合并时只能合并相邻的两堆,代价为两堆石子的个数之和。问把这n堆石子合并成一堆需要的最小代价是多少。
思路:我们可以想象合并的最后一步过程,一定是将两堆石子合并成一堆。我们用dp[i][j] 表示合并第 i 堆到第 j 堆石子的最小代价,用k分开两堆石子来模拟这个过程,这样状态转移方程就变成了dp[i][k] + dp[k+1][j] + sum[j] - sum[i-1]
最后转移方程;dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + sum[j] - sum[i-1]);
其中sum[i]表示前i堆石子的总个数。
看一下代码
#include<cstdio>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x3fffffff
const int N = 205;
int a[N], sum[N], dp[N][N];
int main() {
int n, i, j;
while(~scanf("%d",&n) && n) {
sum[0] = 0;
for(i = 1; i <= n; i++) {
scanf("%d",&a[i]);
dp[i][i] = 0;
sum[i] = sum[i-1] + a[i];
}
for(int l = 2; l <= n; l++) { //枚举合并的堆数
for(i = 1; i <= n - l + 1; i++) { //枚举起始点
j = i + l - 1;
dp[i][j] = INF;
for(int k = i; k <= j; k++) //枚举中间分隔点
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + sum[j] - sum[i-1]);
}
}
printf("%d\n",dp[1][n]);
}
return 0;
}
再看两道我做的vj的题目
第一道题目dp[i][j]就代表从i到j这些晚会我需要穿的衣服的最少数量,第二道题目dp[i][j]代表从i到j这个区间我所匹配到的匹配的括号的最大数量,从这就可以看出这两道题都符合我们设了两个小区间来求最优解的区间dp的要求,题目都比较简单,也都符合我一开始说的模板,只要看ac代码就能明白,就是第二个题有点坑,我一开始没用memset进行初始化清零处理结果就wa了,所以一定要注意细节!细节!细节!重要事情说三遍
Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning to attend as many parties as he can. Since it's Halloween, these parties are all costume parties, Gappu always selects his costumes in such a way that it blends with his friends, that is, when he is attending the party, arranged by his comic-book-fan friends, he will go with the costume of Superman, but when the party is arranged contest-buddies, he would go with the costume of 'Chinese Postman'.
Since he is going to attend a number of parties on the Halloween night, and wear costumes accordingly, he will be changing his costumes a number of times. So, to make things a little easier, he may put on costumes one over another (that is he may wear the uniform for the postman, over the superman costume). Before each party he can take off some of the costumes, or wear a new one. That is, if he is wearing the Postman uniform over the Superman costume, and wants to go to a party in Superman costume, he can take off the Postman uniform, or he can wear a new Superman uniform. But, keep in mind that, Gappu doesn't like to wear dresses without cleaning them first, so, after taking off the Postman uniform, he cannot use that again in the Halloween night, if he needs the Postman costume again, he will have to use a new one. He can take off any number of costumes, and if he takes off k of the costumes, that will be the last k ones (e.g. if he wears costume A before costume B, to take off A, first he has to remove B).
Given the parties and the costumes, find the minimum number of costumes Gappu will need in the Halloween night.
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case starts with a line containing an integer N (1 ≤ N ≤ 100) denoting the number of parties. Next line contains Nintegers, where the ith integer ci (1 ≤ ci ≤ 100) denotes the costume he will be wearing in party i. He will attend party 1 first, then party 2, and so on.
Output
For each case, print the case number and the minimum number of required costumes.
Sample Input
2
4
1 2 1 2
7
1 2 1 1 3 2 1
Sample Output
Case 1: 3
Case 2: 4
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const int MAXN = 100+7;
int n,m;
int num[MAXN];
int dp[MAXN][MAXN];
int main()
{
int t;
cin>>t;
int ca = 0;
while(t--)
{
cin>>n;
for(int i = 1 ; i <= n ; ++i)
cin>>num[i];
for(int i = n ; i >= 1; --i)
for(int j = i ; j <= n ;++j)
{
dp[i][j] = dp[i+1][j] + 1;
for(int k = i+1 ; k <= j ; ++k)
{
if(num[k] == num[i])dp[i][j] = min(dp[i][j],dp[i+1][k-1] + dp[k][j]);
}
}
printf("Case %d: %d\n",++ca,dp[1][n]);
}
}
We give the following inductive definition of a “regular brackets” sequence:
- the empty sequence is a regular brackets sequence,
- if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
- if a and b are regular brackets sequences, then ab is a regular brackets sequence.
- no other sequence is a regular brackets sequence
For instance, all of the following character sequences are regular brackets sequences:
(), [], (()), ()[], ()[()]
while the following character sequences are not:
(, ], )(, ([)], ([(]
Given a brackets sequence of characters a1a2 … an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest m such that for indices i1, i2, …, im where 1 ≤ i1 < i2 < … < im ≤ n, ai1ai2 … aim is a regular brackets sequence.
Given the initial sequence ([([]])]
, the longest regular brackets subsequence is [([])]
.
Input
The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters (
, )
, [
, and ]
; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.
Output
For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.
Sample Input
((()))
()()()
([]])
)[)(
([][][)
end
Sample Output
6
6
4
0
6
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
char a[1010];
int dp[1010][1010];
int main()
{
while(cin>>a&&a[0]!='e')
{
int n=strlen(a);
memset(dp,0,sizeof(dp));
for(int len=1;len<n;len++)
{
for(int i=0,j=len;j<n;i++,j++)
{
if(a[i]=='('&&a[j]==')' || a[i]=='['&&a[j]==']')
{
dp[i][j]=dp[i+1][j-1]+2;
}
for(int x=i;x<j;x++)
{
dp[i][j]=max(dp[i][j],dp[i][x]+dp[x+1][j]);
}
}
}
cout<<dp[0][n-1]<<endl;
}
return 0;
}