区间DP问题
区间DP的两种实现方式:
1.迭代式(当区间为一维时,一般选择迭代式)
2.记忆化搜索式
1、链的区间DP
当存在一条链,链上有 n n n个元素的时候,就是最为典型的迭代式区间DP问题,这里以求最小值为例
#include<bits/stdc++.h>
using namespace std;
const int N = 输入的数值;
int f[N][N];
int a[N];
int main()
{
int n;
cin >> n;
for(int i = 1;i <= n;i++)
{
cin >> a[i];
a[i] += a[i - 1];//利用前缀和处理元素的值
}
memset(f,0x3f,sizeof f);//由于要求最小值,将所有状态初始化为正无穷
for(int len = 1;len <= n;len++)//遍历所有的区间长度
{
for(int i = 1;i + len - 1 <= n;i++)//遍历所有的区间
{
int j = i + len - 1;//i为区间的起始节点,j为区间的尾节点
if(i == j)
f[i][j] = 0;
else
{
for(int k = i;k < j;k++)
f[i][j] = max(f[i][j],f[i][k] + f[k + 1][j] + a[j] - a[i - 1];
}
}
}
cout << f[1][n] << endl;
return 0;
}
2、环的区间DP
当存在一个有 n n n个元素的环的时候,可以将其拉伸为一个长度为 2 n 2n 2n的链,然后利用迭代式处理链的方法处理,这里以最大值为例
#include<bits/stdc++.h>
using namespace std;
const int N = 输入的值 * 2;
int a[N],w[N];
int f[N][N];
int main()
{
int n;
cin >> n;
for(int i = 1;i <= n;i++)
{
cin >> a[i];
a[i + n] = a[i];
}
for(int i = 1;i <= n * 2;i++)
w[i] = w[i - 1] + a[i];//将环拉伸为链
memset(f,-0x3f,sizeof f);
for(int len = 1;len <= n;len++)
{
for(int i = 1;i + len - 1 <= n * 2;i++)
{
int j = i + len - 1;
if(i == j)
f[i][j] = 0;
else
for(int k = i;k < j;k++)
f[i][j] = max(f[i][j],f[i][k] + f[k + 1][j] + w[j] - w[i - 1]);
}
}
int res = -0x3f3f3f3f;
for(int i = 1;i <= n;i++)
res = max(res,f[i][i + n - 1]);
cout << res << endl;
return 0;
}
3、二维区间DP
这里以 棋盘分割为例题来讲解,这道题所运用的是记忆化搜索式的区间DP
首先是公式的推导:
σ = ∑ i = 1 n ( x i − x ˉ ) 2 n \sigma = \sqrt{\frac{\sum_{i=1}^{n}(x_i - \bar{x})^2}{n}} σ=n∑i=1n(xi−xˉ)2
= ∑ i = 1 n x i 2 − ∑ i = 1 n 2 x i x ˉ + ∑ i = 1 n x ˉ 2 n = \sqrt{\frac{\sum_{i=1}^{n}x_i^2 - \sum_{i=1}^{n}2x_i\bar{x} + \sum_{i=1}^{n}\bar{x}^2}{n}} =n∑i=1nxi2−∑i=1n2xixˉ+∑i=1nxˉ2
= ∑ i = 1 n x i 2 − 2 n x ˉ 2 + ∑ i = 1 n x ˉ 2 n = \sqrt{\frac{\sum_{i=1}^{n}x_i^2 - 2n\bar{x}^2 + \sum_{i=1}^{n}\bar{x}^2}{n}} =n∑i=1nxi2−2nxˉ2+∑i=1nxˉ2
= ∑ i = 1 n x i 2 − n x ˉ 2 n = \sqrt{\frac{\sum_{i=1}^{n}x_i^2 - n\bar{x}^2}{n}} =n∑i=1nxi2−nxˉ2
⇒ σ = ( ∑ i = 1 n x i − x ˉ ) 2 n \Rightarrow \sigma = \sqrt{\frac{(\sum_{i=1}^{n}x_i - \bar{x})^2}{n}} ⇒σ=n(∑i=1nxi−xˉ)2
代码如下:
// Problem: 棋盘分割
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/323/
// Memory Limit: 10 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define inf 0x3f3f3f3f
#define eqs 1e-6
#define all(a) a.begin(), a.end()
#define ll long long
#define ull unsigned long long
#define PII pair<int, int>
#define vint vector<int>
#define pb(a) push_back(a)
#define mod 1e9 + 7
const int N = 15, M = 10;
double f[M][M][M][M][N];
int w[M][M];
double X;
int n;
double get(int x1, int y1, int x2, int y2)
{
double sum = w[x2][y2] - w[x1 - 1][y2] - w[x2][y1 - 1] + w[x1 - 1][y1 - 1] - X;
return sum * sum / n;
}
double dp(int x1, int y1, int x2, int y2, int k)
{
double &v = f[x1][y1][x2][y2][k];
if (v >= 0)
return v;
if (k == 1)
return get(x1, y1, x2, y2);
v = inf;
for(int i = x1;i < x2;i++)
{
v = min(v,get(x1,y1,i,y2) + dp(i + 1,y1,x2,y2,k - 1));
v = min(v,get(i + 1,y1,x2,y2) + dp(x1,y1,i,y2,k - 1));
}
for(int i = y1;i < y2;i++)
{
v = min(v,get(x1,y1,x2,i) + dp(x1,i + 1,x2,y2,k - 1));
v = min(v,get(x1,i + 1,x2,y2) + dp(x1,y1,x2,i,k - 1));
}
return v;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= 8; i++)
for (int j = 1; j <= 8; j++)
{
cin >> w[i][j];
w[i][j] += w[i - 1][j] + w[i][j - 1] - w[i - 1][j - 1];
}
X = (double)w[8][8] / n;
memset(f, -1, sizeof f);
printf("%.3f\n",sqrt(dp(1, 1, 8, 8, n)));
return 0;
}