动态规划-区间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; //记录最优点
}
}
}
例题
石子合并
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即可) 时间对比:
回文串
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
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- [Jmeter]---JsonPath表达式提取响应&断言
- envoy filter 开发实践系列 2:官网 http filter 示例编译测试
- 快速入门ElasticSearch
- 如何用 JS 一次获取 HTML 表单的所有字段 ?
- Docker与容器
- Go 每日一库之 air
- Go 每日一库之 goquery
- 个人电脑也做做宏基因组玩玩
- windows 安装 miniconda3 +jupyter lab ,使用系统R语言
- Mysql获取数据的总行数count(*)很慢
- 并发编程问题为什么都很诡异
- 6轮Jmeter压测对比keep-alive的影响
- 多个你不知道的 CSS 居中方案!
- 深入理解Pod(二)
- JavaScript 中 10 个需要掌握基础的问题