动态规划-区间DP

时间:2022-07-25
本文章向大家介绍动态规划-区间DP,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

文章目录

  • 区间DP
  • 四边形不等式优化
  • 例题
    • 石子合并
    • 回文串

区间DP

//朴素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]);//状态转移方程
    }
}   

四边形不等式优化

//四边形不等式优化参考(最小值)
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.

#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;
}

原创不易,请勿转载本不富裕的访问量雪上加霜 ) 博主首页:https://blog.csdn.net/qq_45034708