动态规划-区间DP

区间DP

区间DP主要是先在小区间进行DP得到最优解,然后再利用小区间的最优解合并求大区间的最优解。一般至少需要两层循环枚举所有子区间,复杂度至少是 O ( n 2 ) O(n^2)

d p [ i ] [ j ] dp[i][j] 表示下标位置 i i j j 的所有元素合并所获最大价值
d p [ i ] [ j ] = m a x ( d p [ i ] [ j ] , d p [ i ] [ k ] + d p [ k + 1 ] [ j ] + c o s t [ i ] [ j ] ) dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]+cost[i][j])
k k 是枚举 i i ~ j j 的分割点, c o s t cost 是把这两组元素合并起来的代价。

//朴素DP参考
for (int i = 1; i <= n; i++)dp[i][i]=0;
for (int len = 1; len <= n; len++){	  //枚举区间长度
    for (int i = 1; i <= n - len; i++){	//枚举区间的起点
        int j = i + len;	//根据起点和长度得出终点
        for(int k = i; k < j; k++)	//枚举分割点
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + cost[i][j]);//状态转移方程
    }
}   

四边形不等式优化

在上述代码中的复杂度是 O ( n 3 ) O(n^3) ,只能处理规模 n < 250 n<250 的问题,那么是否可以优化?
在这三重循环中,前两重是枚举所有可能的合并,无法优化。最后一重枚举分割点 k k 是可以优化的,因为最后一重循环是寻找某个子区间的最优分割点,但该操作在多个子区间里是重复的,我们找个这个最优点并记录下来,避免重复计算,进而降低复杂度。
s [ i ] [ j ] s[i][j] 表示区间 [ i , j ] [i,j] 的最优分割点,那么第三重循环范围是 [ s [ i ] [ j 1 ] , s [ i + 1 ] [ j ] ] [s[i][j-1],s[i+1][j]] ,其中 s [ ] [ ] s[][] 是更新dp的时候同时维护。优化后复杂度近 O ( n 2 ) O(n^2) ,可以处理 n < 3000 n<3000 的规模,以空间换时间。

详细四边形不等式优化证明
四边形不等式:设函数 w ( x , y ) w(x,y) 是定义在 Z Z 上的函数,如果对于任意 a b c d a\leq b\leq c \leq d 都有 w ( a , d ) + w ( b , c ) w ( a , c ) + w ( b , d ) w(a,d)+w(b,c) \geq w(a,c)+w(b,d) ,则称函数 w w 满足四边形不等式 。

当问题求最小值时可以用四边形不等式优化,但最大值不满足单调性不能用其优化,此时决策
d p M a x [ i ] [ j ] = m a x ( d p M a x [ i ] [ i ] + d p M a x [ i + 1 ] [ j ] , d p M a x [ j ] [ j ] + d p M a x [ i ] [ j 1 ] ) dpMax[i][j] = max(dpMax[i][i] + dpMax[i + 1][j], dpMax[j][j] + dpMax[i][j - 1])
即考虑i和j这两个位置合并后的最大值,证明参考

//四边形不等式优化参考(最小值)
for (int i = 1; i <= n; i++){
	dp[i][i]=0;
	s[i][i]=i;  //初始化
}
for (int len = 1; len <= n; len++){	
    for(int i = 1; i <= n - len; i++){
        int j = i + len;
        for(int k = s[i][j - 1]; k <= s[i + 1][j]; k++)	//优化缩小范围
        	if(dp[i][j] > dp[i][k] + dp[k + 1][j] + cost[i][j]){
        		dp[i][j] = dp[i][k] + dp[k + 1][j] + cost[i][j];
		   		s[i][j] = k;	//记录最优点
		    }
    }
}   

例题

石子合并

HRBUST - 1818

Description
一条直线上摆放着一行共n堆的石子。现要将石子有序地合并成一堆。规定每次只能选相邻的两堆合并成新的一堆,并将新的一堆石子数记为该次合并的得分。请编辑计算出将n堆石子合并成一堆的最小得分和将n堆石子合并成一堆的最大得分。
Input
输入有多组测试数据。
每组第一行为n(n<=100),表示有n堆石子,。
二行为n个用空格隔开的整数,依次表示这n堆石子的石子数量ai(0<ai<=100)
Output
每组测试数据输出有一行。输出将n堆石子合并成一堆的最小得分和将n堆石子合并成一堆的最大得分。 中间用空格分开。
Sample Input
3
1 2 3
Sample Output
9 11

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn = 102;
int n, cost[maxn];
int dpMin[maxn][maxn], dpMax[maxn][maxn];
void solve() {		//O(n^3)
	for (int i = 1; i <= n; i++)
		dpMin[i][i] = dpMax[i][i] = 0;
	for (int len = 1; len <= n; len++)
		for (int i = 1; i <= n - len; i++) {
			int j = i + len;
			dpMin[i][j] = inf;
			dpMax[i][j] = -inf;
			for (int k = i; k < j; k++) {
				dpMin[i][j] = min(dpMin[i][j], dpMin[i][k] + dpMin[k + 1][j] + cost[j] - cost[i - 1]);
				dpMax[i][j] = max(dpMax[i][j], dpMax[i][k] + dpMax[k + 1][j] + cost[j] - cost[i - 1]);
			}
		}
}
void solve2() {		//O(n^2)
	int s[maxn][maxn];
	for (int i = 1; i <= n; i++) {
		dpMin[i][i] = dpMax[i][i] = 0;
		s[i][i] = i;
	}
	for (int len = 1; len <= n; len++) {
		for (int i = 1; i <= n - len; i++) {
			int j = i + len;
			dpMin[i][j] = inf;
			dpMax[i][j] = -inf;
			dpMax[i][j] = max(dpMax[i][i] + dpMax[i + 1][j], dpMax[j][j] + dpMax[i][j - 1]) + cost[j] - cost[i - 1];
			for (int k = s[i][j - 1]; k <= s[i + 1][j]; k++)	//四边形优化求最小值
				if (dpMin[i][j] > dpMin[i][k] + dpMin[k + 1][j] + cost[j] - cost[i - 1]) {
					dpMin[i][j] = dpMin[i][k] + dpMin[k + 1][j] + cost[j] - cost[i - 1];
					s[i][j] = k;
				}
		}
	}
}
int main(){
	while (cin >> n) {
		cost[0] = 0;
		for (int i = 1; i <= n; i++) {
			cin >> cost[i];	
			cost[i] += cost[i - 1]; 	//前缀和
		}
		//solve();
		solve2();
		cout << dpMin[1][n] << " " << dpMax[1][n] << "\n";
	}
	return 0;
}

(如果排列是环形,扩展至2*n即可)
时间对比:
在这里插入图片描述

在这里插入图片描述

回文串

POJ - 3280

Description
Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow an electronic ID tag that the system will read as the cows pass by a scanner. Each ID tag’s contents are currently a single string with length M (1 ≤ M ≤ 2,000) characters drawn from an alphabet of N (1 ≤ N ≤ 26) different symbols (namely, the lower-case roman alphabet).
Cows, being the mischievous creatures they are, sometimes try to spoof the system by walking backwards. While a cow whose ID is “abcba” would read the same no matter which direction the she walks, a cow with the ID “abcb” can potentially register as two different IDs (“abcb” and “bcba”).
FJ would like to change the cows’s ID tags so they read the same no matter which direction the cow walks by. For example, “abcb” can be changed by adding “a” at the end to form “abcba” so that the ID is palindromic (reads the same forwards and backwards). Some other ways to change the ID to be palindromic are include adding the three letters “bcb” to the begining to yield the ID “bcbabcb” or removing the letter “a” to yield the ID “bcb”. One can add or remove characters at any location in the string yielding a string longer or shorter than the original string.
Unfortunately as the ID tags are electronic, each character insertion or deletion has a cost (0 ≤ cost ≤ 10,000) which varies depending on exactly which character value to be added or deleted. Given the content of a cow’s ID tag and the cost of inserting or deleting each of the alphabet’s characters, find the minimum cost to change the ID tag so it satisfies FJ’s requirements. An empty ID tag is considered to satisfy the requirements of reading the same forward and backward. Only letters with associated costs can be added to a string.
Input
Line 1: Two space-separated integers: N and M
Line 2: This line contains exactly M characters which constitute the initial ID string
Lines 3…N+2: Each line contains three space-separated entities: a character of the input alphabet and two integers which are respectively the cost of adding and deleting that character.
Output
Line 1: A single line with a single integer that is the minimum cost to change the given name tag.
Sample Input
3 4
abcb
a 1000 1100
b 350 700
c 200 800
Sample Output
900
Hint
If we insert an “a” on the end to get “abcba”, the cost would be 1000. If we delete the “a” on the beginning to get “bcb”, the cost would be 1100. If we insert “bcb” at the begining of the string, the cost would be 350 + 200 + 350 = 900, which is the minimum.

给定长度为 m m ,由 n n 个小写字母组成的字符串 s s ,任意增删使其变成回文串,增删不同字母的花费不同,求最小花费。
首先插入和删除这两种操作是等价的(这头加=那头减),取两种操作最小值即可,用 c o s t [ ] cost[] 表示字符的花费。
解法一
看到回文,可以考虑最长公共子序列LCS,把 s s 反转得到 s s' ,求 s s s s' 的LCS的长度 l l ,答案就是 s s 的长度减去 l l 对应花费,LCS和LIS可以参考我的另一篇博客。

解法二
d p [ i ] [ j ] dp[i][j] 表示 s s 子区间 [ i , j ] [i,j] 变成回文的最小花费,如下3种情况:

  1. s[i]==s[j],那么dp[i][j]=dp[i+1][j-1];
  2. dp[i+1][j]是回文串,那么加上字符s[i]的花费就好了,dp[i][j]=dp[i+1][j]+cost[i];
  3. dp[i][j-1]是回文串,那么加上字符s[j]的花费就好了,dp[i][j]=dp[i][j-1]+cost[j];

即状态转移方程为:
d p [ i ] [ j ] = m i n ( d p [ i + 1 ] [ j ] + c o s t [ i ] , d p [ i ] [ j 1 ] + c o s t [ j ] ) dp[i][j]=min(dp[i+1][j]+cost[i],dp[i][j-1]+cost[j])

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 2003;
int n, m, dp[maxn][maxn], cost[26], x, y;
char s[maxn], ch;
int main() {
	while (cin >> n >> m) {
		cin >> s;
		for (int i = 0; i < n; i++) {
			cin >> ch >> x >> y;
			cost[ch - 'a'] = min(x, y);
		}
		for (int i = m - 1; i >= 0; i--)
			for (int j = i + 1; j < m; j++)
				if (s[i] == s[j])
					dp[i][j] = dp[i + 1][j - 1];
				else
					dp[i][j] = min(dp[i + 1][j] + cost[s[i] - 'a'], dp[i][j - 1] + cost[s[j] - 'a']);
		cout << dp[0][m - 1] << "\n";
	}
	return 0;
}

原创不易,请支持正版。(百度发现我的好多博客被抄袭qswl
博主首页:https://blog.csdn.net/qq_45034708
你的点赞将会是我最大的动力,关注一波

猜你喜欢

转载自blog.csdn.net/qq_45034708/article/details/107676869
今日推荐