写这篇文章的目的主要是想总结下区间DP的经典题目,同时给自己复习巩固这方面知识点。
一、定义
区间DP是线性动态规划的扩展,适用场景为每段区间的最优解可以通过更小区间的最优解得到。所以我们一般的解题思路都是先在小区间得到最优解,然后总结出递推公式,利用小区间的最优解求大区间的最优解。
二、实现伪代码
//mst(dp,0) 初始化dp数组
for(int i=1;i<=n;i++)
{
dp[i][i]=初始值
}
for(int len=2;len<=n;len++) //枚举区间长度
for(int i=1;i<=n;i++) //枚举起点
{
int j=i+len-1; //区间终点
if(j>n) break; //越界结束
for(int k=i;k<j;k++) //枚举分割点,构造状态转移方程
{
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]+w[i][j]);
}
}
三、经典例题
1. 石子合并问题
1.1 石子合并(一)
石子合并(一)
Time Limit: 1000 MS Memory Limit: 65535 K
Problem Description
有N堆石子排成一排,每堆石子有一定的数量。现要将N堆石子并成为一堆。合并的过程只能每次将相邻的两堆石子堆成一堆,每次合并花费的代价为这两堆石子的和,经过N-1次合并后成为一堆。求出总的代价最小值。
Input
有多组测试数据,输入到文件结束。
每组测试数据第一行有一个整数n,表示有n堆石子。
接下来的一行有n(0<n<200)个数,分别表示这n堆石子的数目,用空格隔开
Output
输出总代价的最小值,占单独的一行
Examples
Input
3
1 2 3
7
13 7 8 16 21 4 18
Output
9
239
【题目链接】link
【思路】
我们dp[i][j]来表示合并第i堆到第j堆石子的最小代价。
那么状态转移方程为
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+w[i][j]);
其中w[i][j]表示把(i,j)和(k+1,j)这两部分合并起来的代价,即从第i堆到第j堆石子个数的和,为了方便查询并提高效率,我们可以利用前缀和预处理,用sum[i]表示从第1堆到第i堆的石子个数和,那么w[i][j]=sum[j]-sum[i-1]。
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
typedef long long ll;
const int maxn = 205;
const ll mod = 1e9+7;
const ll INF = 1e18;
const double eps = 1e-9;
int n,x;
int sum[maxn];
int dp[maxn][maxn];
int main()
{
while(~scanf("%d",&n))
{
sum[0]=0;
mst(dp,0x3f);
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
sum[i]=sum[i-1]+x;
dp[i][i]=0;
}
for(int len=2;len<=n;len++)
for(int i=1;i<=n;i++)
{
int j=i+len-1;
if(j>n) continue;
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;
}
【拓展】平行四边形优化
上面的代码运行时间在240ms左右,通过这题完全没问题,但我们还可以考虑优化。
由于状态转移时是三重循环的,我们想能否把其中一层优化呢?尤其是枚举分割点的那个,显然我们用了大量的时间去寻找这个最优分割点,所以我们考虑把这个点找到后保存下来
用s[i][j]表示区间[i,j]中的最优分割点,那么第三重循环可以从[i,j-1)优化到【s[i][j-1],s[i+1][j]】。(这个时候小区间s[i][j-1]和s[i+1][j]的值已经求出来了,然后通过这个循环又可以得到s[i][j]的值)。
关于平行四边形优化的证明可以参考这篇博客: link
// 32ms
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
typedef long long ll;
const int maxn = 205;
const ll mod = 1e9+7;
const ll INF = 1e18;
const double eps = 1e-9;
int n,x;
int sum[maxn];
int dp[maxn][maxn];
int s[maxn][maxn];
int main()
{
while(~scanf("%d",&n))
{
sum[0]=0;
mst(dp,0x3f);
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
sum[i]=sum[i-1]+x;
dp[i][i]=0;
s[i][i]=i;
}
for(int len=2;len<=n;len++)
for(int i=1;i<=n;i++)
{
int j=i+len-1;
if(j>n) continue;
for(int k=s[i][j-1];k<=s[i+1][j];k++)
{
if(dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1]<dp[i][j])
{
dp[i][j]=dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1];
s[i][j]=k;
}
}
}
printf("%d\n",dp[1][n]);
}
return 0;
}
1.2 Monkey Party
Monkey Party
Time Limit: 2000 MS Memory Limit: 65535 K
Problem Description
Far away from our world, there is a banana forest. And many lovely monkeys livethere. One day, SDH(Song Da Hou), who is the king of banana forest, decides tohold a big party to celebrate Crazy Bananas Day. But the little monkeys don’tknow each other, so as the king, SDH must do something.
Now there are n monkeys sitting in a circle, and each monkey has a makingfriends time. Also, each monkey has two neighbor. SDH wants to introduce themto each other, and the rules are:
1.every time, he can only introduce one monkey and one of this monkey’sneighbor.
2.if he introduce A and B, then every monkey A already knows will know everymonkey B already knows, and the total time for this introducing is the sum ofthe making friends time of all the monkeys A and B already knows;
3.each little monkey knows himself;
In order to begin the party and eat bananas as soon as possible, SDH want toknow the mininal time he needs on introducing.
Input
There is several test cases. In each case, the first line is n(1 ≤ n ≤ 1000), whichis the number of monkeys. The next line contains n positive integers(less than1000), means the making friends time(in order, the first one and the last oneare neighbors). The input is end of file.
Output
For each case, you should print a line giving the mininal time SDH needs on introducing.
Examples
Input
8
5 2 4 7 6 1 3 9
Output
105
【题目链接】link
【题意】
问题转化后其实就是环形石子合并,即现在有围成一圈的若干堆石子,其他条件跟前面那题相同,问合并所需最小代价——上一题的升级版
【思路】
我们需要做的是尽量向简单的问题转化,可以把前n-1堆石子一个个移到第n个后面,那样环就变成了线,即现在有2*n-1堆石子需要合并,我们只要求下面的式子即可。求法与上面那题完全一样。
min(dp[s][s+n-1]),s ∈ \in ∈[1,n]
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
typedef long long ll;
const int maxn = 1005*2;
const ll mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
int a[maxn];
int sum[maxn];
int dp[maxn][maxn];
int s[maxn][maxn];
int main()
{
int n;
while(~scanf("%d",&n))
{
sum[0]=0;
mst(dp,0x3f);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
s[i][i]=i;
dp[i][i]=0;
}
for(int i=1;i<n;i++)
{
sum[i+n]=sum[i+n-1]+a[i];
s[i+n][i+n]=i+n;
dp[i+n][i+n]=0;
}
for(int len=2;len<=n;len++)
for(int i=1;i<=2*n-1;i++)
{
int j=i+len-1;
if(j>2*n-1) break;
for(int k=s[i][j-1];k<=s[i+1][j];k++)
{
if(dp[i][j]>dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1])
{
dp[i][j]=dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1];
s[i][j]=k;
}
}
}
int ans=INF;
for(int i=1;i<=n;i++)
{
ans=min(ans,dp[i][i+n-1]);
}
printf("%d\n",ans);
}
return 0;
}
2. 括号匹配问题
2.1 Brackets
Brackets
Time Limit: 1000 MS Memory Limit: 65536 K
Problem 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 brackets sequence, then (s) and [s] are regular brackets sequences, and if a andb are regular brackets sequences, thenab 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 thefollowing 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 ofs. That is, you wish to find the largestmsuch that for indicesi1,i2, …,imwhere 1 ≤i1 <i2 < … <im≤n,ai1ai2 … aim is a regular bracketssequence.
Given the initial sequence ([([]])], the longest regularbrackets subsequence is[([])].
Input
The input test file will contain multiple test cases. Each input test case consists of asingle 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.
Examples
Input
((()))
()()()
([]])
)[)(
([][][)
end
Output
6
6
4
0
6
【题目链接】link
【题意】
给出一个的只有’(‘,’)‘,’[‘,’]’四种括号组成的字符串,求最多有多少个括号满足题目里所描述的完全匹配。
【思路】
用dp[i][j]表示区间[i,j]里最大完全匹配数。只要得到了dp[i][j],那么就可以根据比较s[i-1]与s[j+1]是否匹配得到dp[i-1][j+1]
dp[i-1][j+1]=dp[i][j]+(s[i-1]与[j+1]匹配 ? 2 : 0)
然后利用状态转移方程更新一下区间最优解即可。
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j])
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
typedef long long ll;
const int maxn = 105;
const ll mod = 1e9+7;
const ll INF = 1e18;
const double eps = 1e-9;
char s[maxn];
int dp[maxn][maxn];
int main()
{
while(~scanf("%s",s+1)&&s[1]!='e')
{
int n=strlen(s+1);
mst(dp,0);
for(int len=2;len<=n;len++)
for(int i=1;i<=n;i++)
{
int j=i+len-1;
if(j>n) break;
if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']')
{
dp[i][j]=dp[i+1][j-1]+2;
}
for(int k=i;k<j;k++)
{
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]);
}
}
printf("%d\n",dp[1][n]);
}
return 0;
}
2.2 括号匹配(2)
括号匹配(2)
Time Limit: 1000 MS Memory Limit: 65536 K
Problem Description
给你一个字符串,里面只包含”(“,”)“,”[“,”]”四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来。
如:
[]是匹配的
([])[]是匹配的
((]是不匹配的
([)]是不匹配的
Input
第一行输入一个正整数N,表示测试数据组数(N<=10)
每组测试数据都只有一行,是一个字符串S,S中只包含以上所说的四种字符,S的长度不超过100
Output
对于每组测试数据都输出一个正整数,表示最少需要添加的括号的数量。每组测试输出占一行
Examples
Input
4
[]
([])[]
((]
([)]
Output
0
0
3
2
【题目链接】link
【题意】
上一题求的是满足完美匹配的最大括号数量,而这题问的是使所有括号完美匹配需要添加的最小括号数量
【思路】
显然,要使添加的括号尽量少,我们需要使原来的括号序列尽可能多得匹配,即先求最大匹配数量(跟上题一样),那么还剩下一些没有匹配的括号,我们就需要依次加上一个括号使它们得到匹配。综上所述,所求=原序列括号数量-最大匹配括号数量。(因此此题的代码与上题几乎一致)。
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
typedef long long ll;
const int maxn = 105;
const ll mod = 1e9+7;
const ll INF = 1e18;
const double eps = 1e-9;
char s[maxn];
int dp[maxn][maxn];
int main()
{
rush()
{
scanf("%s",s+1);
int n=strlen(s+1);
mst(dp,0);
for(int len=2;len<=n;len++)
for(int i=1;i<=n;i++)
{
int j=i+len-1;
if(j>n) break;
if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']')
{
dp[i][j]=dp[i+1][j-1]+2;
}
for(int k=i;k<j;k++)
{
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]);
}
}
printf("%d\n",n-dp[1][n]);
}
return 0;
}
3. 整数划分问题
整数划分(四)
Time Limit: 1000 MS Memory Limit: 65536 K
Problem Description
暑假来了,hrdv又要留学校在参加ACM集训了,集训的生活非常Happy(ps:你懂得),可是他最近遇到了一个难题,让他百思不得其解,他非常郁闷。。亲爱的你能帮帮他吗?
问题是我们经常见到的整数划分,给出两个整数 n , m ,要求在 n 中加入m – 1 个乘号,将n分成m段,求出这m段的最大乘积
Input
第一行是一个整数T,表示有T组测试数据
接下来T行,每行有两个正整数 n,m ( 1<= n < 10^19, 0 < m <= n的位数)
Output
输出每组测试样例结果为一个整数占一行
Examples
Input
2
111 2
1111 2
Output
11
121
【题目链接】link
【题意】
给出一个数n,要求在n的数位间插入(m-1)个乘号,将n分成了m段,求这m段的最大乘积。
【思路】
用dp[i][j]表示从第一位到第i位共插入j个乘号后乘积的最大值。根据区间DP的思想我们可以从插入较少乘号的结果算出插入较多乘号的结果,在放第j个乘号的时候枚举放的位置
状态转移方程为
dp[i][j]=max(dp[i][j],dp[k][j-1]*num[k+1][i])
其中num[i][j]表示从s[i]到s[j]这段连续区间代表的数值,可以提前进行预处理得到
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
typedef long long ll;
const int maxn = 25;
const ll mod = 1e9+7;
const ll INF = 1e18;
const double eps = 1e-9;
int m;
char s[maxn];
ll dp[maxn][maxn];
ll num[maxn][maxn];
int main()
{
rush()
{
scanf("%s%d",s+1,&m);
mst(dp,0);
int len=strlen(s+1);
for(int i=1;i<=len;i++)
{
num[i][i]=s[i]-'0';
for(int j=i+1;j<=len;j++)
{
num[i][j]=num[i][j-1]*10+s[j]-'0';
}
}
for(int i=1;i<=len;i++)
{
dp[i][0]=num[1][i];
}
for(int j=1;j<m;j++)
for(int i=j+1;i<=len;i++)
for(int k=j;k<i;k++)
{
dp[i][j]=max(dp[i][j],dp[k][j-1]*num[k+1][i]);
}
printf("%lld\n",dp[len][m-1]);
}
return 0;
}
4. 凸多边形三角划分问题
三角划分
Time Limit: 1000 MS Memory Limit: 65536 K
Problem Description
给定一个具有N(N<=50)个顶点(从1到N编号)的凸多边形,每个顶点的权值已知。问如何把这个凸多边形划分成N-2个互不相交的三角形,使得这些三角形顶点的权值的乘积之和最小。
Input
第一行为顶点数N,第二行为N个顶点(从1到N)的权值
Output
乘积之和的最小值。题目保证结果在int范围内。
Examples
Input
5
1 6 4 2 1
5
121 122 123 245 231
Output
34
12214884
【思路】
用dp[i][j]表示从顶点i到顶点j的凸多边形三角剖分后所得到的最小乘积。
那么可以写出状态转移方程,并通过枚举分割点来转移
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]);
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
typedef long long ll;
const int maxn = 55;
const ll mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
int a[maxn];
int dp[maxn][maxn];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
mst(dp,0);
for(int len=3;len<=n;len++)
for(int i=1;i<=n;i++)
{
int j=i+len-1;
if(j>n) break;
dp[i][j]=INF;
for(int k=i+1;k<j;k++)
{
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]);
}
}
printf("%d\n",dp[1][n]);
}
return 0;
}
四、拓展例题
题目一
Cake slicing
Time Limit: 1000 MS Memory Limit: 32768 K
Problem Description
A rectangular cake with a grid of m*n unit squares on its top needs to be slicedinto pieces. Several cherries are scattered on the top of the cake with at mostone cherry on a unit square. The slicing should follow the rules below:
- each piece is rectangular or square;
- each cutting edge is straight and along a grid line;
- each piece has only one cherry on it;
- each cut must split the cake you currently cut two separate parts
For example, assume that the cake has a grid of 3*4 unit squares on its top,and there are three cherries on the top, as shown in the figure below.
One allowable slicing is as follows.
For this way of slicing , the total length of the cutting edges is 2+4=6.
Another way of slicing is
In this case, the total length of the cutting edges is 3+2=5.
Give the shape of the cake and the scatter of the cherries , you are supposedto find
out the least total length of the cutting edges.
Input
The input file contains multiple test cases. For each test case:
The first line contains three integers , n, m and k (1≤n, m≤20), where n*m isthe size of the unit square with a cherry on it . The two integers showrespectively the row number and the column number of the unit square in thegrid .
All integers in each line should be separated by blanks.
Output
Output an integer indicating the least total length of the cutting edges.
Examples
Input
3 4 3
1 2
2 3
3 2
Output
Case 1: 5
【题目链接】link
【题意】
有一个n*m大小的蛋糕,上面有k个樱桃,现在我们需要把这个蛋糕切成k份,使每份蛋糕上有一个樱桃,问最小切割长度和(切割一刀必须切到底)
【思路】
用dp[i][j][k][l]表示以(i,j)为左上角,(k,l)为右下角的矩形切成每份一个樱桃的最小切割长度。然后就利用区间DP,枚举切割点,从小区间转移到大区间。由于这道题不同区间樱桃个数不同,故用递归的写法会方便些。
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
typedef long long ll;
const int maxn = 25;
const ll mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
int n,m,k;
int dp[maxn][maxn][maxn][maxn];
bool flag[maxn][maxn]; //记录某个点是否有樱桃
int fun(int a,int b,int c,int d)
{
if(dp[a][b][c][d]!=-1) //已经计算过
{
return dp[a][b][c][d];
}
int cnt=0;
for(int i=a;i<=c;i++)
for(int j=b;j<=d;j++)
{
if(flag[i][j])
cnt++;
}
if(cnt<=1) //区域内樱桃个数小于2,那么不用切割
{
return dp[a][b][c][d]=0;
}
int Min=INF;
for(int i=a;i<c;i++) //横着切
{
Min=min(Min,fun(a,b,i,d)+fun(i+1,b,c,d)+(d-b+1));
}
for(int i=b;i<d;i++) //竖着切
{
Min=min(Min,fun(a,b,c,i)+fun(a,i+1,c,d)+(c-a+1));
}
return dp[a][b][c][d]=Min;
}
int main()
{
int cas=1;
int x,y;
while(~scanf("%d%d%d",&n,&m,&k))
{
mst(dp,-1);
mst(flag,0);
for(int i=0;i<k;i++)
{
scanf("%d%d",&x,&y);
flag[x][y]=1;
}
int ans=fun(1,1,n,m);
printf("Case %d: %d\n",cas++,ans);
}
return 0;
}
题目二
String painter
Time Limit: 2000 MS Memory Limit: 32768 K
Problem Description
There are two strings A and B with equal length. Both strings are made up of lowercase letters. Now you have a powerful string painter. With the help of thepainter, you can change a segment of characters of a string to any othercharacter you want. That is, after using the painter, the segment is made up of only one kind of character. Now your task is to change A to B using string painter. What’s the minimum number of operations?
Input
Input contains multiple cases. Each case consists of two lines:
The first line contains string A.
The second line contains string B.
The length of both strings will not be greater than 100.
Output
A single line contains one integer representing the answer.
Examples
Input
zzzzzfzzzzz
abcdefedcba
abababababab
cdcdcdcdcdcd
Output
6
7
【题目链接】link
【题意】
给出字符串A和B,每一次我们可以使用一种颜色(用字母代替)刷任意一个连续子序列,问将字符串A变成字符串B最少需要刷多少次。
【思路】
分析了很久,发现直接去考虑将A串刷成B串非常困难。于是我们考虑间接转化。
用dp[i][j]表示把将一个空白串[i,j]刷成B字符串对应位置的最小次数。
用ans[i]表示把A串的区间[1,i]刷成B串需要的最小次数。
然后状态转移一下就OK啦。
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
typedef long long ll;
const int maxn = 105;
const ll mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
char s1[maxn],s2[maxn];
int dp[maxn][maxn];
int ans[maxn];
int main()
{
while(~scanf("%s%s",s1+1,s2+1))
{
int n=strlen(s1+1);
mst(dp,0);
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
{
dp[i][j]=j-i+1;
}
for(int j=1;j<=n;j++)
for(int i=j;i>=1;i--)
{
dp[i][j]=dp[i+1][j]+1;
for(int k=i+1;k<=j;k++)
{
if(s2[i]==s2[k]) //这样的话s2[i]可以跟左半区间一起刷到
{
dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]);
}
}
}
for(int i=1;i<=n;i++)
{
ans[i]=dp[1][i];
if(s1[i]==s2[i])
{
ans[i]=min(ans[i],ans[i-1]);
}
else
for(int j=1;j<i;j++)
{
ans[i]=min(ans[i],ans[j]+dp[j+1][i]); //已得出的最优解+剩下的按空白串去刷
}
}
printf("%d\n",ans[n]);
}
return 0;
}
2022/07/05:排版优化
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/34643.html