线性DP
满足从左往右身高递减, 从上到下身高递减, 编号小的身高最高, 求方案数
动态规划是对暴力搜索的优化, 使用集合表示一类方案, 考虑集合与集合之间的关系
- 性质1: 在每一排中, 排好的人一定是从左边开始的连续的一段
- 性质2: 从上到下没排人数单调递减
因为最多有排, 因此使用维数组, 表示当前排好的形状
- 代表从后往前每排人数分别为的所有方案的集合, 其中;
- 的值是该集合中元素的数量
考虑将最后一个同学放在哪一排, 将集合进行划分
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 30;
int k;
LL f[N][N][N][N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
while (cin >> k, k) {
int arr[5] = {0};
memset(f, 0, sizeof f);
for (int i = 0; i < k; ++i) cin >> arr[i];
f[0][0][0][0][0] = 1;
for (int a = 0; a <= arr[0]; ++a) {
for (int b = 0; b <= min(a, arr[1]); ++b) {
for (int c = 0; c <= min(b, arr[2]); ++c) {
for (int d = 0; d <= min(c, arr[3]); ++d) {
for (int e = 0; e <= min(c, arr[4]); ++e) {
LL &tmp = f[a][b][c][d][e];
if (a && a - 1 >= b) tmp += f[a - 1][b][c][d][e];
if (b && b - 1 >= c) tmp += f[a][b - 1][c][d][e];
if (c && c - 1 >= d) tmp += f[a][b][c - 1][d][e];
if (d && d - 1 >= e) tmp += f[a][b][c][d - 1][e];
if (e) tmp += f[a][b][c][d][e - 1];
}
}
}
}
}
LL res = f[arr[0]][arr[1]][arr[2]][arr[3]][arr[4]];
cout << res << "\n";
}
return 0;
}当, 枚举倒数第二个位置
理论上时间复杂度:
对代码进行等价变形, 优化到
第三层循环的含义是: 在到中找到最大的
使用一个变量边循环边计算下一层的最大值
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 3010;
int n;
int arr1[N], arr2[N];
int f[N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> arr1[i];
for (int i = 1; i <= n; ++i) cin >> arr2[i];
for (int i = 1; i <= n; ++i) {
int max_val = 1;
for (int j = 1; j <= n; ++j) {
f[i][j] = f[i - 1][j];
if (arr1[i] == arr2[j]) f[i][j] = max(f[i][j], max_val);
if (arr1[i] > arr2[j]) max_val = max(max_val, f[i][j] + 1);
}
}
int res = 0;
for (int i = 1; i <= n; ++i) res = max(res, f[n][i]);
cout << res << "\n";
return 0;
}性质: 一定存在一组使得每个数字都在数组中出现过
- 将原序列从小到大排序得到
- 考虑所有在和中的位置, 例如上述粉色框
- 统计横坐标中小于等于的个数计为, 大于等于的个数计为
分为三种情况
- 那么将粉色框整体想下移直到第一个点落在, 结果会变好
- 那么讲粉色框整体向上移知道第一个落在, 结果会变好
- 无论向上移动或者向下移动结果都不会变差
因此可以通过有限次操作将所有移动到上
动态规划: 代表已经分配好了前个并且第个位置分配的数字是的所有方案
枚举倒数第二个数字是选择的哪个数字进行转移, 在计算的时候, 维护记录前个位置分配的最小值, 这样当计算到当前位置的时候, 就能得到位置的最小代价, 就不需要在进行枚举了
上述绿色框是一样的, 因此使用一个变量维护这个最小值, 时间复杂度
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2010, INF = 0x3f3f3f3f;
int n;
int arr[N], _arr[N];
int f[N][N];
int handle() {
for (int i = 1; i <= n; ++i) _arr[i] = arr[i];
// _arr就是排好序的aj
sort(_arr + 1, _arr + n + 1);
for (int i = 1; i <= n; ++i) {
int min_val = INF;
for (int j = 1; j <= n; ++j) {
min_val = min(min_val, f[i - 1][j]);
f[i][j] = min_val + abs(_arr[j] - arr[i]);
}
}
int res = INF;
for (int i = 1; i <= n; ++i) res = min(res, f[n][i]);
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> arr[i];
int res = handle();
reverse(arr + 1, arr + n + 1);
res = min(res, handle());
cout << res << "\n";
return 0;
}直观的想法, 处理完前个请求, 并且三个服务员位置在的所有情况
我们发现, 处理完一个请求后, 一定有一个服务员在该位置, 因此可以将状态表示优化为, 也就是另外两个服务员位置是
因为当前状态转移到的状态最多只有三种, 因此使用当前状态更新下一个状态
时间复杂度
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 210, OP_NUM = 1010, INF = 0x3f3f3f3f;
int n, op_num;
int w[N][N], pos[OP_NUM];
int f[OP_NUM][N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> op_num;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cin >> w[i][j];
}
}
for (int i = 1; i <= op_num; ++i) cin >> pos[i];
memset(f, 0x3f, sizeof f);
pos[0] = 3;
f[0][1][2] = 0;
for (int i = 0; i < op_num; ++i) {
for (int x = 1; x <= n; ++x) {
for (int y = 1; y <= n; ++y) {
int z = pos[i];
int curr = f[i][x][y];
if (x == y || x == z || y == z) continue;
int ne_pos = pos[i + 1];
f[i + 1][x][y] = min(f[i + 1][x][y], curr + w[z][ne_pos]);
f[i + 1][x][z] = min(f[i + 1][x][z], curr + w[y][ne_pos]);
f[i + 1][y][z] = min(f[i + 1][y][z], curr + w[x][ne_pos]);
}
}
}
int res = INF;
for (int x = 1; x <= n; ++x) {
for (int y = 1; y <= n; ++y) {
int z = pos[op_num];
if (x == y || x == z || y == z) continue;
res = min(res, f[op_num][x][y]);
}
}
cout << res << "\n";
return 0;
}将问题转化为两个人一起从开始位置走, 不能走到同一个位置, 走到最下面的好感度最大值
直观的想法就是, 从左上走到和的最大好感度, 但是时间复杂度很大, 需要进行优化
考虑什么情况会相互影响, 优化成, 表示横纵坐标之和, 这样就可以优化查为级别的
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 60;
int row, col;
int w[N][N];
int f[N << 1][N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> row >> col;
for (int i = 1; i <= row; ++i) {
for (int j = 1; j <= col; ++j) {
cin >> w[i][j];
}
}
for (int k = 1; k <= row + col; ++k) {
for (int x1 = 1; x1 < k; ++x1) {
for (int x2 = 1; x2 < k; ++x2) {
int y1 = k - x1;
int y2 = k - x2;
if (y1 < 1 || y1 > col || y2 < 1 || y2 > col) continue;
int &curr = f[k][x1][x2];
int val = w[x1][y1] + (y1 == y2 ? 0 : w[x2][y2]);
curr = max(curr, f[k - 1][x1 - 1][x2 - 1] + val);
curr = max(curr, f[k - 1][x1 - 1][x2] + val);
curr = max(curr, f[k - 1][x1][x2 - 1] + val);
curr = max(curr, f[k - 1][x1][x2] + val);
}
}
}
int res = f[row + col][row][row];
cout << res << "\n";
return 0;
}贪心 + 线性DP
按时间从小到大排序总时间最小
证明: 调整法, 找到相邻的前一个比后一个大的两个位置
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int n, w[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) cin >> w[i];
sort(w, w + n);
LL res = 0, tmp = n;
for (int i = 0; i < n; ++i) {
res += w[i] * --tmp;
}
cout << res << "\n";
return 0;
}- 如果最小值是, 那么将都去掉, 方案数等于
- 如果最小值大于, 那么将所有选择的数字都减去, 方案数等于
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1010, MOD = 1e9 + 7;
int n;
// f[i][j]表示总和是i并且由j个数组成的所有方案
int f[N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
f[0][0] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
f[i][j] = (f[i - 1][j - 1] + f[i - j][j]) % MOD;
}
}
int res = 0;
for (int i = 1; i <= n; ++i) res = (res + f[n][i]) % MOD;
cout << res << "\n";
return 0;
}根据排序不等式, 给怨气大的小朋友分配更多的饼干
表示给前个小朋友分配个饼干的所有方案
怨气值之和只和相对大小有关, 因此能代表最小值大于的情况
边分为两种情况, 如果最值大于, 表示将所有人的饼干 时间复杂度:
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef pair<int, int> PII;
const int N = 35, M = 5010;
int n, m;
PII g[N];
int s[N];
// 给前i个孩子分配j个饼干的所有方案中怨气之和最小值
int f[N][M], res[N];
int main () {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> g[i].first;
g[i].second = i;
}
// 将怨气值从大到小排序
sort(g + 1, g + n + 1);
reverse(g + 1, g + n + 1);
for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + g[i].first;
memset(f, 0x3f, sizeof f);
f[0][0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (j >= i) f[i][j] = f[i][j - i];
for (int k = 1; k <= i && k <= j; ++k) {
int val = f[i - k][j - k] + (s[i] - s[i - k]) * (i - k);
f[i][j] = min(f[i][j], val);
}
}
}
cout << f[n][m] << "\n";
int i = n, j = m, offset = 0;
while (i && j) {
if (j >= i && f[i][j] == f[i][j - i]) {
offset++;
j -= i;
}
// 枚举最后连续个1的个数
else {
for (int k = 1; k <= i && k <= j; ++k) {
int val = f[i - k][j - k] + (s[i] - s[i - k]) * (i - k);
// 统计最后连续个1的个数
if (f[i][j] == val) {
for (int l = i - k + 1; l <= i; ++l) res[g[l].second] += 1 + offset;
i -= k, j -= k;
break;
}
}
}
}
for (int i = 1; i <= n; ++i) cout << res[i] << " ";
cout << "\n";
return 0;
}推不等式, 按照从小到大的顺序吃能量和是最大的
然后将问题转化为背包问题
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 110, M = 110 * 110;
int cases;
int n;
struct Node {
int t, w, l;
bool operator< (const Node &u) const {
return t * u.l < l * u.t;
}
} stones[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> cases;
for (int k = 1; k <= cases; ++k) {
cin >> n;
int m = 0;
for (int i = 1; i <= n; ++i) {
cin >> stones[i].t >> stones[i].w >> stones[i].l;
m += stones[i].t;
}
sort(stones + 1, stones + n + 1);
// f[i][j]表示考虑前i个能量石总耗时是j的所有方案中能量和的最大值
int f[N][M] = {0};
int res = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= m; ++j) {
f[i][j] = f[i - 1][j];
if (j >= stones[i].t) {
int val = max(0, f[i - 1][j - stones[i].t] + stones[i].w - (j - stones[i].t) * stones[i].l);
f[i][j] = max(f[i][j], val);
}
res = max(res, f[i][j]);
}
}
printf("Case #%d: %d\n", k, res);
}
return 0;
}线性DP之子序列问题
最少用多少个非上升子序列能覆盖所有导弹?
在子序列中结尾的数字越大越好, 因为大的可以兼容小的, 因此对于当前导弹高度应该加入到大于等于的最小的位置
贪心的流程
- 如果当前所有子序列的结尾都严格小于, 那么创建一个新的子序列
- 如果存在一个大于等于的最小的结尾, 那么讲当前导弹加入到该位置
如何实现? 创建代表已经开好的子序列的结尾的导弹高度, 一定是单调上升的
假设找到大于等于的最小的数是, 并且, 将当前位置加入到位置后面, 因为是大于等于的最小的数, 因此加入后不等式结果不会改变, 因此是非严格单调上升的
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1010;
int n;
int w[N], f[N];
int g[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int val;
while (cin >> val) w[++n] = val;
// 计算最长非上升子序列的长度
int res = 1;
for (int i = 1; i <= n; ++i) {
int val = w[i];
f[i] = 1;
for (int j = 1; j < i; ++j) {
if (val <= w[j]) f[i] = max(f[i], f[j] + 1);
}
res = max(res, f[i]);
}
cout << res << "\n";
int cnt = 0;
for (int i = 1; i <= n; ++i) {
int k = 0;
while (k < cnt && g[k] < w[i]) k++;
if (k == cnt) cnt++;
g[k] = w[i];
}
cout << cnt << "\n";
return 0;
}因为既有上升还有下降并且导弹数量小于等于, 因此可用通过进行暴力搜索, 迭代加深, 剪枝, 贪心进行优化 时间复杂度
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 60;
int n;
int w[N];
int up[N], down[N];
int res;
void dfs(int u, int up_cnt, int down_cnt) {
if (up_cnt + down_cnt >= res) return;
if (u == n) {
res = min(res, up_cnt + down_cnt);
return;
}
int k = 0;
while (k < up_cnt && w[u] <= up[k]) k++;
int val = up[k];
up[k] = w[u];
if (k == up_cnt) dfs(u + 1, up_cnt + 1, down_cnt);
else {
dfs(u + 1, up_cnt, down_cnt);
up[k] = val;
}
k = 0;
while (k < down_cnt && w[u] >= down[k]) k++;
val = down[k];
down[k] = w[u];
if (k == down_cnt) dfs(u + 1, up_cnt, down_cnt + 1);
else {
dfs(u + 1, up_cnt, down_cnt);
down[k] = val;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
while (cin >> n, n) {
for (int i = 0; i < n; ++i) cin >> w[i];
res = n;
dfs(0, 0, 0);
cout << res << "\n";
}
return 0;
}背包问题
背包问题
观察状态转移方程: 每一层状态都和上一层状态有关, 因此可以优化掉一维使用滚动数组的方式进行优化
为什么体积从大到小枚举? 观察状态转移方程如果体积从小到大枚举, 那么在计算之前, 已经被计算过了, 也就不是层的数据, 如果从大到小枚举会避免该问题的发生
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1010;
int n, m;
int v[N], w[N];
int f[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> v[i] >> w[i];
for (int i = 1; i <= n; ++i) {
for (int j = m; j >= v[i]; --j) {
f[j] = max(f[j], f[j - v[i]] + w[i]);
}
}
cout << f[m] << "\n";
return 0;
}完全背包问题
每种物品可选择的数量是无限个 朴素写法时间复杂度, 观察状态转移方程之间的关系, 优化到
因此可以将状态转移方程优化为
因为也是使用到的是层的状态, 因此可以优化掉一维, 但是从小到大枚举体积, 因为用到的是第层的状态
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1010;
int n, m;
int v[N], w[N];
int f[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> v[i] >> w[i];
for (int i = 1; i <= n; ++i) {
for (int j = v[i]; j <= m; ++j) {
f[j] = max(f[j], f[j - v[i]] + w[i]);
}
}
cout << f[m] << "\n";
return 0;
}多重背包问题及二进制优化
状态表示与完全背包一致, 但是多重背包有物品数量限制, 因此不能用完全背包问题的优化方式, 先考虑二进制优化
因为任意整数都能表示成二进制形式, 那么对于某种物品的某种选择都可以对应一个不大于的二进制数进行拼凑, 因此可以将物品进行拆分成份, 然后转化为背包问题
因为将物品分为份, 时间复杂度, 是物品最大数量
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1000 * 12 + 10, M = 2010;
int n, m;
int vols[N], weights[N];
int f[M];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
int cnt = 1;
for (int i = 0; i < n; ++i) {
int v, w, s;
cin >> v >> w >> s;
int tmp = 1;
while (tmp <= s) {
vols[cnt] = tmp * v;
weights[cnt++] = tmp * w;
s -= tmp;
tmp <<= 1;
}
if (s) {
vols[cnt] = s * v;
weights[cnt++] = s * w;
}
}
for (int i = 1; i <= cnt; ++i) {
for (int j = m; j >= vols[i]; --j) {
f[j] = max(f[j], f[j - vols[i]] + weights[i]);
}
}
cout << f[m] << "\n";
return 0;
}多重背包问题线性优化(单调队列优化)
其中
假设物品数量上限是, 那么求的就是红色窗口中的最大值再加偏移量, 求的就是前面的窗口内的最大值
如果当前计算, 求的就是红色框中的最大值
如果当前计算, 求的就是这四个的最大值, 如果物品上限是, 那么在求的时候整个红色窗口向后移动, 也就是求滑动窗口的最大值
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1010, M = 20010;
int n, m;
int v[N], w[N], s[N];
int queue[M];
// 因为当前层状态只与上一层状态有关, 因此_f记录上一层状态
int f[M], _f[M];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> v[i] >> w[i] >> s[i];
for (int i = 1; i <= n; ++i) {
memcpy(_f, f, sizeof f);
for (int r = 0; r < v[i]; ++r) {
int head = 0, tail = -1;
for (int j = r; j <= m; j += v[i]) {
while (head <= tail && j - queue[head] > s[i] * v[i]) head++;
while (head <= tail && _f[j] >= _f[queue[tail]] + (j - queue[tail]) / v[i] * w[i]) tail--;
queue[++tail] = j;
f[j] = _f[queue[head]] + (j - queue[head]) / v[i] * w[i];
}
}
}
cout << f[m] << "\n";
return 0;
}为什么在加入之后更新?
因为计算的是上一层的窗口内的最大值, 如果不将当前入队, 可能导致最大值错误, 必须将入队后再统计最大值
二维费用的背包问题
朴素状态表示, 前个物品中选, 总体积不超过, 并且总重量不超过的所有方案中价值最大的方案
状态转移方程
因为当前状态只与上一层状态有关, 因此可以优化掉一维, 但是体积和重量需要从大到小枚举, 时间复杂度
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1010, M = 110;
int n, v, m;
int vols[N], weights[N], vals[N];
int f[M][M];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> v >> m;
for (int i = 1; i <= n; ++i) cin >> vols[i] >> weights[i] >> vals[i];
for (int i = 1; i <= n; ++i) {
for (int j = v; j >= vols[i]; --j) {
for (int k = m; k >= weights[i]; --k) {
f[j][k] = max(f[j][k], f[j - vols[i]][k - weights[i]] + vals[i]);
}
}
}
cout << f[v][m] << "\n";
return 0;
}
分组背包问题
同一组的物品只能选择一个 状态表示:, 从前个物品组中选, 总体积不大于的所有方案
状态计算: 对于第组物品分为选或者不选, 如果不选, 如果选择第物品, 继续细分选择组内的哪个物品
时间复杂度
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define v first
#define w second
typedef pair<int, int> PII;
const int N = 110, M = 110;
int n, m;
int s[N];
PII arr[N][N];
int f[M];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> s[i];
for (int j = 0; j < s[i]; ++j) cin >> arr[i][j].v >> arr[i][j].w;
}
for (int i = 1; i <= n; ++i) {
for (int j = m; j >= 0; --j) {
for (int k = 0; k < s[i]; ++k) {
int v = arr[i][k].v;
int w = arr[i][k].w;
if (j >= v) f[j] = max(f[j], f[j - v] + w);
}
}
}
cout << f[m] << "\n";
return 0;
}注意 一定要先循环体积再循环组内物品, 如果先循环组内物品, 可能被同组物品更新过, 导致组内不是只能选择一个物品, 因此必须先循环体积再循环组内物品
混合背包问题
混合背包问题可以将每个物品都看做多重背包问题, 线性解决该问题
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1010, M = 1010;
int n, m;
int v[N], w[N], s[N];
int f[M], _f[M];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
int val;
cin >> v[i] >> w[i];
cin >> val;
if (val == -1) val = 1;
else if (val == 0) val = m / v[i];
s[i] = val;
}
int queue[M];
for (int i = 1; i <= n; ++i) {
memcpy(_f, f, sizeof f);
for (int r = 0; r < v[i]; ++r) {
int head = 0, tail = -1;
for (int j = r; j <= m; j += v[i]) {
while (head <= tail && j - queue[head] > s[i] * v[i]) head++;
while (head <= tail && _f[j] >= _f[queue[tail]] + (j - queue[tail]) / v[i] * w[i]) tail--;
queue[++tail] = j;
f[j] = _f[queue[head]] + (j - queue[head]) / v[i] * w[i];
}
}
}
cout << f[m] << "\n";
return 0;
}树上背包问题/有依赖的背包问题
如果选择一个物品, 必须选择该物品的父节点 状态表示: 表示以为根节点的子树, 容量不超过的情况下, 的最大价值
每个物品枚举体积, 然后对该位置的子节点也枚举体积, 有个物品, 因此时间复杂度
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 110, M = N * N;
int n, m;
int v[N], w[N];
int head[N], edge_end[M], next_edge[M], edge_index;
int f[N][M];
void add(int ver1, int ver2) {
edge_end[edge_index] = ver2, next_edge[edge_index] = head[ver1], head[ver1] = edge_index++;
}
void dfs(int u) {
for (int i = v[u]; i <= m; ++i) f[u][i] = w[u];
for (int i = head[u]; ~i; i = next_edge[i]) {
int ver = edge_end[i];
dfs(ver);
for (int j = m; j >= v[u]; --j) {
// 枚举分配给子节点的体积, 决策选择哪个子节点价值最大
for (int k = 0; k <= j - v[u]; ++k) {
f[u][j] = max(f[u][j], f[u][j - k] + f[ver][k]);
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
memset(head, -1, sizeof head);
cin >> n >> m;
int root = 0;
for (int i = 1; i <= n; ++i) {
int fa;
cin >> v[i] >> w[i] >> fa;
if (fa == -1) root = i;
else add(fa, i);
}
dfs(root);
cout << f[root][m] << "\n";
return 0;
}倒序枚举体积, 要保证使用的是上一个递归层级的数据
背包问题求方案数
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1010, M = 1010;
const int MOD = 1e9 + 7;
int n, m;
int v[N], w[N];
int f[M], g[M];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> v[i] >> w[i];
g[0] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = m; j >= v[i]; --j) {
int val = max(f[j], f[j - v[i]] + w[i]);
int tmp = 0;
if (val == f[j]) tmp = (tmp + g[j]) % MOD;
if (val == f[j - v[i]] + w[i]) tmp = (tmp + g[j - v[i]]) % MOD;
f[j] = val;
g[j] = tmp;
}
}
int res = 0;
for (int i = 0; i <= m; ++i) {
if (f[i] == f[m]) res = (res + g[i]) % MOD;
}
cout << res << "\n";
return 0;
}注意, 如果, 方案数也要进行累加
背包问题求具体方案
背包问题求字典序最小方案, 如果从前向后枚举物品, 那么在求方案的时候是从后往前推, 得到的不是字典序最小方案, 因此枚举物品需要从后往前枚举, 这样倒推得到的就是字典序最小方案
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1010, M = 1010;
int n, m;
int v[N], w[N];
int f[N][M];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> v[i] >> w[i];
for (int i = n; i >= 1; --i) {
for (int j = 0; j <= m; ++j) {
f[i][j] = f[i + 1][j];
if (j >= v[i]) f[i][j] = max(f[i][j], f[i + 1][j - v[i]] + w[i]);
}
}
// f[1][m]是最终状态
int i = 1, j = m;
while (i <= n && j >= 0) {
int val = f[i][j];
if (j >= v[i] && val == f[i + 1][j - v[i]] + w[i]) {
cout << i << " ";
j -= v[i];
i++;
}
else i++;
}
return 0;
}注意在状态转移过程中保证, 否则计算的结果是错误的
背包问题综合应用
因为对于当前物品组的物品选择情况是互斥的决策, 因此枚举选择情况如果只有一个附属物品, 那就是选或者不选两种情况
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
#define x first
#define y second
typedef pair<int, int> PII;
const int N = 70, M = 32010;
int n, m;
// 分别记录主件和附件
PII arr[N];
vector<PII> s[N];
int f[M];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> m >> n;
for (int i = 1; i <= n; ++i) {
int v, w, u;
cin >> v >> w >> u;
w *= v;
if (!u) arr[i] = {v, w};
else s[u].push_back({v, w});
}
for (int i = 1; i <= n; ++i) {
for (int j = m; j >= 0; --j) {
// 枚举附属件的选择情况
for (int k = 0; k < 1 << s[i].size(); ++k) {
int v = arr[i].x, w = arr[i].y;
// 枚举是否选择了当前附属件
for (int l = 0; l < s[i].size(); ++l) {
if (k >> l & 1) {
v += s[i][l].x;
w += s[i][l].y;
}
}
if (j >= v) f[j] = max(f[j], f[j - v] + w);
}
}
}
cout << f[m] << "\n";
return 0;
}区间DP
区间DP模板
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 310;
int n, w[N];
int f[N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
int val;
cin >> val;
w[i] = w[i - 1] + val;
}
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;
if (len == 1) f[i][j] = 0;
for (int k = i; k < j; ++k) f[i][j] = min(f[i][j], f[i][k] + f[k + 1][j] + w[j] - w[i - 1]);
}
}
cout << f[1][n] << "\n";
return 0;
}区间DP之破环成链
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 55 << 1, INF = 0x3f3f3f3f;
int n;
char op[N];
int w[N];
// 记录最大值和最小值
int f[N][N], g[N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> op[i] >> w[i];
op[i + n] = op[i];
w[i + n] = w[i];
}
for (int len = 1; len <= n; ++len) {
for (int i = 1; i + len - 1 <= 2 * n; ++i) {
int j = i + len - 1;
if (len == 1) f[i][j] = g[i][j] = w[i];
else {
f[i][j] = -INF, g[i][j] = INF;
for (int k = i; k < j; ++k) {
int val1 = f[i][k], val2 = f[k + 1][j];
int val3 = g[i][k], val4 = g[k + 1][j];
// 当前计算的是加法
if (op[k + 1] == 't') {
f[i][j] = max(f[i][j], val1 + val2);
g[i][j] = min(g[i][j], val3 + val4);
}
// 当前计算的是乘法
else {
int maxv = -INF, minv = INF;
int tmp[4] = {
val1 * val2,
val1 * val4,
val3 * val2,
val3 * val4
};
for (int l = 0; l < 4; ++l) maxv = max(maxv, tmp[l]), minv = min(minv, tmp[l]);
f[i][j] = max(f[i][j], maxv);
g[i][j] = min(g[i][j], minv);
}
}
}
}
}
int res = -INF;
for (int i = 1; i <= n; ++i) res = max(res, f[i][i + n - 1]);
cout << res << "\n";
for (int i = 1; i <= n; ++i) {
if (f[i][i + n - 1] == res) cout << i << " ";
}
cout << "\n";
return 0;
}区间DP之树的遍历顺序
对于一棵树的序列, 长度一定是也就是奇数
如何划分集合?
按照最后一棵子树所占的区间, 因为终点一定是, 因此可以枚举起点, 有可能存在这一类树, 乘法原理计算方案数, 因为序列的长度一定是奇数, 因此枚举的步长是
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 310, MOD = 1e9;
int n;
char str[N];
LL f[N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> str + 1;
n = strlen(str + 1);
// 如果是偶数 不合法
if (n % 2 == 0) {
cout << 0 << "\n";
return 0;
}
for (int len = 1; len <= n; len += 2) {
for (int i = 1; i + len - 1 <= n; ++i) {
int j = i + len - 1;
if (len == 1) f[i][j] = 1;
// 计算[i, j]之间能形成的树的数量
else if (str[i] == str[j]) {
// 枚举最后一个子树开始位置k
for (int k = i; k < j - 1; k += 2) {
// 因为保证最后一个点是根节点也就是str[j], 因此最后一个子树结尾位置就是j - 1
if (str[k] == str[j]) {
LL val = f[i][k] * f[k + 1][j - 1] % MOD;
f[i][j] = (f[i][j] + val) % MOD;
}
}
}
}
}
cout << f[1][n] << "\n";
return 0;
}#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 40;
int n, w[N];
int f[N][N], fa[N][N];
void dfs(int l, int r) {
if (l > r) return;
int k = fa[l][r];
cout << k << " ";
dfs(l, k - 1);
dfs(k + 1, r);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> w[i];
for (int len = 1; len <= n; ++len) {
for (int i = 1; i + len - 1 <= n; ++i) {
int j = i + len - 1;
for (int k = i; k <= j; ++k) {
int lmax = k == i ? 1 : f[i][k - 1];
int rmax = k == j ? 1 : f[k + 1][j];
int val = w[k];
if (i == j) {
f[i][j] = val;
fa[i][j] = i;
}
else {
val += lmax * rmax;
if (val > f[i][j]) {
f[i][j] = val;
fa[i][j] = k;
}
}
}
}
}
cout << f[1][n] << "\n";
dfs(1, n);
return 0;
}区间DP + 单调队列优化DP
枚举长度为的长度, 枚举起始距离
每一个位置存储的是, 也就是当前位置油量减下一个距离, 能够走完一圈等价于前缀和是否是大于等于的, 也就是所有长度为区间内前缀和的最小值是否是大于等于
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 2e6 + 10;
int n;
int o[N], d[N];
LL s[N], q[N];
bool res[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> o[i] >> d[i];
s[i] = s[i + n] = o[i] - d[i];
}
for (int i = 1; i <= 2 * n; ++i) s[i] += s[i - 1];
// 队头维护前缀和最小值
int h = 0, t = -1;
for (int i = 2 * n; i >= 0; --i) {
while (h <= t && q[h] - i + 1 > n) h++;
while (h <= t && s[i] <= s[q[t]]) t--;
q[++t] = i;
if (i <= n && s[q[h]] - s[i - 1] >= 0) res[i] = true;
}
// 逆时针
d[0] = d[n];
for (int i = 1; i <= n; ++i) s[i] = s[i + n] = o[i] - d[i - 1];
// 计算后缀和
for (int i = 2 * n; i >= 0; --i) s[i] += s[i + 1];
h = 0, t = -1;
for (int i = 1; i <= 2 * n; ++i) {
while (h <= t && i - q[h] + 1 > n) h++;
while (h <= t && s[i] <= s[q[t]]) t--;
q[++t] = i;
if (i >= n + 1 && s[q[h]] - s[i + 1] >= 0) res[i - n] = true;
}
for (int i = 1; i <= n; ++i) {
res[i] ? cout << "TAK" << "\n" : cout << "NIE" << "\n";
}
return 0;
}
树形DP/换根DP
找到一条路径使得路径两端的距离最远
将所有路径进行分类, 将路径按照最高点划分集合
如何统计最大长度?
对于当前根节点, 经过该点的最长路径就是当前点的最长路 + 次长路
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 10010, M = N << 1;
int n, m;
int head[N], edge_end[M], next_edge[M], w[M], edge_index;
int res = 0;
void add(int ver1, int ver2, int val) {
edge_end[edge_index] = ver2, next_edge[edge_index] = head[ver1], w[edge_index] = val, head[ver1] = edge_index++;
}
int dfs(int u, int fa) {
int d1 = 0, d2 = 0;
for (int i = head[u]; ~i; i = next_edge[i]) {
int ver = edge_end[i];
if (ver == fa) continue;
int dis = dfs(ver, u) + w[i];
if (dis > d1) d2 = d1, d1 = dis;
else if (dis > d2) d2 = dis;
}
res = max(res, d1 + d2);
return d1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
memset(head, -1, sizeof head);
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int u, v, w;
cin >> u >> v >> w;
add(u, v, w);
add(v, u, w);
}
dfs(1, -1);
cout << res << "\n";
return 0;
}换根DP模板
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 100010, M = N << 1, INF = 0x3f3f3f3f;
int n;
int head[N], edge_end[M], next_edge[M], w[M], edge_index;
int d1[N], d2[N], son[N];
int up[N];
void add(int ver1, int ver2, int val) {
edge_end[edge_index] = ver2, next_edge[edge_index] = head[ver1], w[edge_index] = val, head[ver1] = edge_index++;
}
void dfs1(int u, int fa) {
d1[u] = d2[u] = 0;
for (int i = head[u]; ~i; i = next_edge[i]) {
int ver = edge_end[i];
if (ver == fa) continue;
dfs1(ver, u);
int val = d1[ver] + w[i];
if (val > d1[u]) {
d2[u] = d1[u];
d1[u] = val;
son[u] = ver;
}
else if (val > d2[u]) d2[u] = val;
}
}
void dfs2(int u, int fa) {
for (int i = head[u]; ~i; i = next_edge[i]) {
int ver = edge_end[i];
if (ver == fa) continue;
int val = (son[u] == ver ? max(up[u], d2[u]) : max(up[u], d1[u])) + w[i];
up[ver] = max(up[ver], val);
dfs2(ver, u);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
memset(head, -1, sizeof head);
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int u, v, w;
cin >> u >> v >> w;
add(u, v, w);
add(v, u, w);
}
dfs1(1, -1);
dfs2(1, -1);
int res = INF;
for (int i = 1; i <= n; ++i) {
int val = max(up[i], d1[i]);
res = min(res, val);
}
cout << res << "\n";
return 0;
}换根DP应用
预处理节点所在子树的大小, 和节点深度, 处理后进行状态的初始化, 然后换根
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10, M = N << 1;
int n;
int head[N], edge_end[M], next_edge[M], edge_index;
int sz[N], depth[N];
//f[i]表示以i为根节点的所有节点的深度之和
LL f[N];
void add(int ver1, int ver2) {
edge_end[edge_index] = ver2, next_edge[edge_index] = head[ver1], head[ver1] = edge_index++;
}
void dfs1(int u, int fa) {
sz[u] = 1, depth[u] = depth[fa] + 1;
for (int i = head[u]; ~i; i = next_edge[i]) {
int ver = edge_end[i];
if (ver == fa) continue;
dfs1(ver, u);
sz[u] += sz[ver];
}
}
void dfs2(int u, int fa) {
for (int i = head[u]; ~i; i = next_edge[i]) {
int ver = edge_end[i];
if (ver == fa) continue;
f[ver] = f[u] + n - 2 * sz[ver];
dfs2(ver, u);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
memset(head, -1, sizeof head);
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
add(u, v);
add(v, u);
}
dfs1(1, 0);
for (int i = 1; i <= n; ++i) f[1] += depth[i];
dfs2(1, -1);
LL max_val = f[1];
int res = 1;
for (int i = 2; i <= n; ++i) {
if (f[i] > max_val) {
max_val = f[i];
res = i;
}
}
cout << res << "\n";
return 0;
}树形DP应用
如何计算边权为的树的直径?
- 首先从任意点计算能到达的最远的点
- 可以证明一定是某条直径的端点
如果新树的直径更长, 一定包含或, 因此确定了新树直径的一个端点
- 如果距离最长那就是直径
- 否则找到另一个端点, 那么到和到之间一定存在一条直径
如何快速求, 倍增求树上两个节点的距离, 预处理每个点到根的距离, 然后计算树上差分
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1000010, M = 20;
int n, m;
int fa[N][M], depth[N];
int lca(int u, int v) {
if (depth[u] < depth[v]) swap(u, v);
for (int i = M - 1; i >= 0; --i) {
if (depth[fa[u][i]] >= depth[v]) u = fa[u][i];
}
if (u == v) return v;
for (int i = M - 1; i >= 0; --i) {
if (fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
return fa[u][0];
}
int get_dist(int u, int v) {
int p = lca(u, v);
return depth[u] + depth[v] - 2 * depth[p];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> m;
for (int i = 2; i <= 4; ++i) fa[i][0] = 1, depth[i] = 1;
n = 4;
int res = 2;
int s_node = 2, e_node = 3;
while (m--) {
int u;
cin >> u;
// 新的子节点
int x = n + 1, y = n + 2;
n += 2;
fa[x][0] = fa[y][0] = u;
depth[x] = depth[y] = depth[u] + 1;
// 初始化lca
for (int k = 1; k < M; ++k) {
fa[x][k] = fa[fa[x][k - 1]][k - 1];
fa[y][k] = fa[fa[y][k - 1]][k - 1];
}
// 如果新加入的点的直径更长一定包含x或者y 如果不包含那么就是旧树
int da = get_dist(s_node, x);
int db = get_dist(e_node, x);
// 记录新的直径的两个端点和直径长度
if (da > db && da > res) res = da, e_node = x;
else if (da <= db && db > res) res = db, s_node = x;
cout << res << "\n";
}
return 0;
}


























