0x53 动态规划-区间DP

A: 石子合并

所求问题:1到n这些石子合并最少需要多少代价
由于石子合并的顺序可以任意,我们将石子分为两个部分
子问题:1到k这堆石子合并,k+1到n这堆石子合并,再把两堆石子合并,需要多少代价\((1<=k<=n)\)

那么便可以得到状态转移方程 $$dp[i][j]=min(dp[i][k]+dp[k+1][j]+……)$$
i到j这些石子合并,代价不仅仅是分开的两堆内部合并的代价,还有两堆石子相互合并的所带来的代价消耗,可得出省略号中的答案为a[i]+a[i+1]+……+a[j]
为了加快效率,我们设立一个sum数组来记录前缀和,那么可得到完整的状态转移方程:

\(dp[i][j]=min(dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1])\)

需要注意的是,由于dp求最小值,需要将dp预处理为无穷大

#include<bits/stdc++.h>
using namespace std;
const int N = 310;
int a[N], n;
int dp[N][N], sum[N];
int main() {
	//freopen("in.txt", "r", stdin);
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	memset(dp, 0x3f, sizeof dp);
	for (int i = 1; i <= n; ++i)cin >> a[i], dp[i][i] = 0, sum[i] = sum[i - 1] + a[i];
	//F[l,r] = min(F[l,k],F[k + 1.r] + sum[l,r]
	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] + sum[j] - sum[i - 1]);
		}
	cout << dp[1][n] << endl;
}

B: Polygon

题目描述

Polygon is a game for one player that starts on a polygon with N vertices, like the one in Figure 1, where N=4. Each vertex is labelled with an integer and each edge is labelled with either the symbol + (addition) or the symbol * (product). The edges are numbered from 1 to N.
img
On the first move, one of the edges is removed. Subsequent moves involve the following steps:

  1. pick an edge E and the two vertices V1 and V2 that are linked by E; and
  2. replace them by a new vertex, labelled with the result of performing the operation indicated in E on the labels of V1 and V2.

The game ends when there are no more edges, and its score is the label of the single vertex remaining.

Consider the polygon of Figure 1. The player started by removing edge 3. After that, the player picked edge 1, then edge 4, and, finally, edge 2. The score is 0.
img
Write a program that, given a polygon, computes the highest possible score and lists all the edges that, if removed on the first move, can lead to a game with that score.

输入描述:

Your program is to read from standard input. The input describes a polygon with N vertices. It contains two lines. On the first line is the number N. The second line contains the labels of edges 1, ..., N, interleaved with the vertices' labels (first that of the vertex between edges 1 and 2, then that of the vertex between edges 2 and 3, and so on, until that of the vertex between edges N and 1), all separated by one space. An edge label is either the letter t (representing +) or the letter x (representing *).

$3 <= N <= 50 $
For any sequence of moves, vertex labels are in the range \([-32768,32767]\).

输出描述:

Your program is to write to standard output. On the first line your program must write the highest score one can get for the input polygon. On the second line it must write the list of all edges that, if removed on the first move, can lead to a game with that score. Edges must be written in increasing order, separated by one space.

输入

4
t -7 t 4 x 2 x 5

输出

33
1 2

解题思路

这道题的题意大致是对于n个点的环先删除一个点后,根据连线的运算符逐渐合并各个点,最后求最大的结果可能是多少。一道经典的区间DP题,有几个点需要注意一下:

  1. 需要枚举最开始删除的是哪条边
  2. 由于一开始是个环,删边后可能从中间断开,导致区间不连续,无法使用区间DP。解决方法是将原始数组复制一倍,这样就能保证能够使用区间DP。
  3. 由于元素可能存在负值,负负相乘得正后可能得到的结果更大。所以在维护结果的时候,既要维护最大值,也要维护最小值。
#include<bits/stdc++.h>
using namespace std;
const int N = 106, INF = 0x3f3f3f3f;
char c[N];
int a[N], dp_max[N][N], dp_min[N][N];
int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		cin >> a[i];
	}
	for (int i = n + 1; i <= (n << 1); i++) {
		c[i] = c[i - n];
		a[i] = a[i - n];
	}
	memset(dp_max, 0xcf, sizeof(dp_max));
	memset(dp_min, 0x3f, sizeof(dp_min));
	for (int i = 1; i <= (n << 1); i++) dp_max[i][i] = dp_min[i][i] = a[i];
	for (int len = 2; len <= n; len++)
		for (int l = 1; l + len - 1 <= (n << 1); l++) {
			int r = l + len - 1;
			for (int k = l + 1; k <= r; k++)
				if (c[k] == 't') {
					dp_max[l][r] = max(dp_max[l][r], dp_max[l][k - 1] + dp_max[k][r]);
					dp_min[l][r] = min(dp_min[l][r], dp_min[l][k - 1] + dp_min[k][r]);
				}
				else {
					dp_max[l][r] = max(dp_max[l][r], max(dp_max[l][k - 1] * dp_max[k][r], dp_min[l][k - 1] * dp_min[k][r]));
					dp_min[l][r] = min(dp_min[l][r], min(dp_max[l][k - 1] * dp_max[k][r], min(dp_min[l][k - 1] * dp_min[k][r], min(dp_max[l][k - 1] * dp_min[k][r], dp_min[l][k - 1] * dp_max[k][r]))));
				}
		}
	int ans = -INF;
	for (int i = 1; i <= n; i++) ans = max(ans, dp_max[i][i + n - 1]);
	cout << ans << endl;
	for (int i = 1; i <= n; i++)
		if (ans == dp_max[i][i + n - 1])
			cout << i << " ";
	return 0;
}

C: 金字塔

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9;
ll f[310][310];
string s;
ll dfs(int l, int r) {
	if (l > r) return 0;//递归边界
	if (l == r) return 1;//递归边界
	if (f[l][r] != -1) return f[l][r];//记忆化
	if (s[l] != s[r]) return 0;
	f[l][r] = 0;
	for (int k = l + 2; k <= r; k++)
		(f[l][r] += dfs(l + 1, k - 1) * dfs(k, r)) %= mod;
	return f[l][r];
}

int main() {
	//freopen("in.txt", "r", stdin);
	memset(f, -1, sizeof f);//-1代表没有计算过
	cin >> s;
	cout << dfs(0, s.length() - 1) << endl;
}
posted @ 2020-08-13 16:30  RioTian  阅读(269)  评论(0编辑  收藏  举报