天天看點

動态規劃-區間DP區間DP四邊形不等式優化例題

文章目錄

  • 區間DP
  • 四邊形不等式優化
  • 例題
    • 石子合并
    • 回文串

區間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]);//狀态轉移方程
    }
}              

複制

四邊形不等式優化

動态規劃-區間DP區間DP四邊形不等式優化例題
//四邊形不等式優化參考(最小值)
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即可)

時間對比:

動态規劃-區間DP區間DP四邊形不等式優化例題
動态規劃-區間DP區間DP四邊形不等式優化例題

回文串

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.

動态規劃-區間DP區間DP四邊形不等式優化例題
#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