区间DP小结(附经典例题)

——这篇文章主要想总结下区间DP的经典题目,同时给自己复习巩固这方面知识点。

 

区间DP

 

一、定义

 

区间DP,顾名思义是在区间上DP,它的主要思想就是先在小区间进行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

 

 

三、经典例题

 

1. 石子合并问题

 

题目链接

石子合并(一)

时间限制:1000 ms  |  内存限制:65535 KB

难度:3

描述 

N堆石子排成一排,每堆石子有一定的数量。现要将N堆石子并成为一堆。合并的过程只能每次将相邻的两堆石子堆成一堆,每次合并花费的代价为这两堆石子的和,经过N-1次合并后成为一堆。求出总的代价最小值。

输入

有多组测试数据,输入到文件结束。
每组测试数据第一行有一个整数n,表示有n堆石子。
接下来的一行有n(0< n <200)个数,分别表示这n堆石子的数目,用空格隔开

输出

输出总代价的最小值,占单独的一行

样例输入

 

3

1 2 3

7

13 7 8 16 21 4 18

样例输出

 

9

239

 

 

 

【思路】

 

我们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堆石子个数的和,为了方便查询,我们可以用sum[i]表示从第1堆到第i堆的石子个数和,那么w[i][j]=sum[j]-sum[i-1].

 

#include 
#include 
#include 
#include 
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

 

【平行四边形优化】

 

上面的代码运行时间在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]的值)。

 

关于平行四边形优化的证明可以参考这篇博客: 戳我戳我

 

【32ms】

 

 

#include 
#include 
#include 
#include 
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]

 

附:此题的升级版  HDOJ 3506 Monkey Party

Monkey Party

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit:131072/65536 K (Java/Others)
Total Submission(s): 1738    Accepted Submission(s): 784

 

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.

 

 

Sample Input

8

5 2 4 7 6 1 3 9

 

 

Sample Output

105

 

 


【题意】

 

问题转化后其实就是环形石子合并,即现在有围成一圈的若干堆石子,其他条件跟其那面那题相同,问合并所需最小代价。

 

【思路】

 

我们需要做的是尽量向简单的问题转化,可以把前n-1堆石子一个个移到第n个后面,那样环就变成了线,即现在有2*n-1堆石子需要合并,我们只要求下面的式子即可。求法与上面那题完全一样。

 

#include 
#include 
#include 
#include 
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;i2*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. 括号匹配问题

 

 

题目链接

 

Brackets

 

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 8710

Accepted: 4659

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 < … <imn,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.

Sample Input

((()))

()()()

([]])

)[)(

([][][)

end

Sample Output

6

6

4

0

6

 

【题意】

 

给出一个的只有'(',')','[',']'四种括号组成的字符串,求最多有多少个括号满足题目里所描述的完全匹配。

 

【思路】

 

用dp[i][j]表示区间[i,j]里最大完全匹配数。

 

只要得到了dp[i][j],那么就可以得到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 
#include 
#include 
#include 
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

 

 

【变题】 题目链接

 

括号匹配(二)

时间限制:1000 ms  |  内存限制:65535 KB

难度:6

描述

给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来。
如:
[]是匹配的
([])[]是匹配的
((]是不匹配的
([)]是不匹配的

输入

第一行输入一个正整数N,表示测试数据组数(N<=10)
每组测试数据都只有一行,是一个字符串S,S中只包含以上所说的四种字符,S的长度不超过100

输出

对于每组测试数据都输出一个正整数,表示最少需要添加的括号的数量。每组测试输出占一行

样例输入

 

4

[]

([])[]

((]

([)]

样例输出

 

0

0

3

2

 

【题意】

 

上一题求的是满足完美匹配的最大括号数量,而这题问的是使所有括号完美匹配需要添加的最小括号数量

 

【思路】

 

显然,要使添加的括号尽量少,我们需要使原来的括号序列尽可能多得匹配,即先求最大匹配数量(跟上题一样),那么还剩下一些没有匹配的括号,我们就需要依次加上一个括号使它们得到匹配。综上所述,所求=原序列括号数量-最大匹配括号数量。(因此此题的代码与上题几乎一致)。

#include 
#include 
#include 
#include 
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

 


3. 整数划分问题

 

 

题目链接

 

整数划分(四)

时间限制:1000 ms  |  内存限制:65535 KB

难度:3

描述

暑假来了,hrdv又要留学校在参加ACM集训了,集训的生活非常Happy(ps:你懂得),可是他最近遇到了一个难题,让他百思不得其解,他非常郁闷。。亲爱的你能帮帮他吗?

问题是我们经常见到的整数划分,给出两个整数 n , m ,要求在 n 中加入m - 1 个乘号,将n分成m段,求出这m段的最大乘积

输入

第一行是一个整数T,表示有T组测试数据
接下来T行,每行有两个正整数 n,m ( 1<= n < 10^19, 0 < m <= n的位数);

输出

输出每组测试样例结果为一个整数占一行

样例输入

2

111 2

1111 2

样例输出

11

121

 

 


【题意】

 

给出一个数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 
#include 
#include 
#include 
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

 


4. 凸多边形三角划分问题

 

Problem Description

给定一个具有N(N<=50)个顶点(从1到N编号)的凸多边形,每个顶点的权值已知。问如何把这个凸多边形划分成N-2个互不相交的三角形,使得这些三角形顶点的权值的乘积之和最小。

 

Input

第一行为顶点数N,第二行为N个顶点(从1到N)的权值。

 

Output

乘积之和的最小值。题目保证结果在int范围内。

 

Sample Input

5

1 6 4 2 1

5

121 122 123 245 231

 

 

 

Sample Output

 

34

12214884

 

 

 

【题意】

 

RT

 

【思路】

 

用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 
#include 
#include 
#include 
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

 

 

 


四、拓展例题

 

1. HDOJ 2513 Cake slicing

 

题目链接

Cake slicing

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

 

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:
1.  each piece is rectangular or square;
2.  each cutting edge is straight and along a grid line;
3.  each piece has only one cherry on it;
4.  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.

 

区间DP小结(附经典例题)_第1张图片

For this way of slicing , the total length of the cutting edges is 2+4=6.
Another way of slicing is

 

区间DP小结(附经典例题)_第2张图片


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.

 

 

Sample Input

3 4 3

1 2

2 3

3 2

 

 

Sample Output

Case 1: 5

 

 

【题意】

 

有一个n*m大小的蛋糕,上面有k个樱桃,现在我们需要把这个蛋糕切成k份,使每份蛋糕上有一个樱桃,问最小切割长度和。(切割一刀必须切到底)

 

【思路】

 

用dp[i][j][k][l]表示以(i,j)为左上角,(k,l)为右下角的矩形切成每份一个樱桃的最小切割长度。然后就利用区间DP的作用,枚举切割点,从小区间转移到大区间。由于这道题不同区间樱桃个数不同,故用递归的写法会方便些。

 

 

 

 

#include 
#include 
#include 
#include 
#include 
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

 

2. HDOJ 2476 String painter

题目链接

String painter

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 4691    Accepted Submission(s): 2208

 

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 ofonly one kind of character. Now your task is to change A to B using stringpainter. 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.

 

 

Sample Input

zzzzzfzzzzz

abcdefedcba

abababababab

cdcdcdcdcdcd

 

 

Sample Output

6

7

 


【题意】

 

给出字符串A和B,每一次我们可以使用一种颜色(用字母代替)刷任意一个连续子序列,问将字符串A变成字符串B最少需要刷多少次。

 

【思路】

 

分析了很久,发现直接去考虑将A串刷成B串非常困难。于是我们考虑间接转化。

 

用dp[i][j]表示把将一个空白串[i,j]刷成B字符串对应位置的最小次数。

 

用ans[i]表示把A串的区间[1,i]刷成B串需要的最小次数。

 

然后状态转移一下就OK啦。

 

 

#include 
#include 
#include 
#include 
#include 
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

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(动态规划)