前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >动态规划-区间DP

动态规划-区间DP

作者头像
唔仄lo咚锵
发布2020-09-15 14:46:09
4100
发布2020-09-15 14:46:09
举报

文章目录

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

区间DP

代码语言:javascript
复制
//朴素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]);//状态转移方程
    }
}   

四边形不等式优化

代码语言:javascript
复制
//四边形不等式优化参考(最小值)
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

代码语言:javascript
复制
#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.

代码语言:javascript
复制
#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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-07-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 区间DP
  • 四边形不等式优化
  • 例题
    • 石子合并
      • 回文串
      相关产品与服务
      对象存储
      对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档