动态规划笔记
bluewindde · · 个人记录
本文应该在 VSCode 上浏览与编辑,其它平台可能会炸 Markdown 或 LaTeX(对此),转载自 GitHub。
链接
动态规划初步·各种子序列问题
题解 P1352 【没有上司的舞会】
写在前面
判断性继承思想:下一状态最优值 = 最优比较函数(已经记录的最优值,可以由先前状态得出的最优值)
全部设状态思想:有不确定性(后效性)的状态全部设进 DP 方程。
时间复杂度越高的算法越全能,就像 DFS,它什么都能干。
当发现题目变数很多但只需要最优结果时,大胆去动归。(P2986 [USACO10MAR] Great Cow Gathering G)
DP 类别
1. 最长上升子序列
n^2 做法
设
答案:所有状态取
求解 P1352 没有上司的舞会代码:
#include <iostream>
#include <vector>
using namespace std;
int n;
vector<int> vec[6005];
int dp[6005][2];
static inline void dfs(int u, int fa) {
for (auto v : vec[u]) {
if (v == fa) continue;
dfs(v, u);
dp[u][1] = max(dp[u][1], max(dp[u][1]+dp[v][0], dp[v][0]));
dp[u][0] = max(max(dp[u][0], dp[u][0] + dp[v][1]), max(dp[v][0], dp[v][1]));
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> dp[i][1];
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
vec[u].push_back(v);
vec[v].push_back(u);
}
dfs(1, 0);
int ans = -1e9;
for (int i = 1; i <= n; ++i) {
ans = max(ans, max(dp[i][0], dp[i][1]));
}
cout << ans << endl;
return 0;
}
选与不选不影响其它
是上一种题型的弱化版。
P1122 最大子树和
题意:一棵树,每个点有权值
设
初始化:
答案:所有状态取
求解 P1122 最大子树和代码:
#include <iostream>
#include <vector>
using namespace std;
int n;
int a[16005];
vector<int> vec[16005];
int dp[16005];
static inline void dfs(int u, int fa) {
for (auto v : vec[u]) {
if (v == fa) continue;
dfs(v, u);
dp[u] += max(dp[v], 0);
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> dp[i];
}
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
vec[u].push_back(v);
vec[v].push_back(u);
}
dfs(1, 0);
int ans = -1e9; // 出题人给你脸了: ans = 0 只有 90 分
for (int i = 1; i <= n; ++i) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
return 0;
}
换根 DP
在第一次 DFS 时预处理一些信息,基于这些信息,在第二次 DFS 时选择合适的根节点进行 DP。
P2986 [USACO10MAR] Great Cow Gathering G
题意:一棵树,每个点有权值
假设每个点都到了节点
求解 P2986 [USACO10MAR] Great Cow Gathering G代码:
#include <iostream>
#include <vector>
#define int long long
using namespace std;
int n, sum;
int c[100005];
int cnt[100005];
int dis[100005];
int dp[100005];
vector<pair<int, int>> vec[100005];
static inline int dfs1(int u, int fa) {
int count = 0;
for (auto [v, w] : vec[u]) {
if (v == fa) continue;
int s = dfs1(v, u);
dis[u] += dis[v] + w * s;
count += s;
}
cnt[u] = c[u] + count;
return cnt[u];
}
static inline void dfs2(int u, int fa) {
for (auto [v, w] : vec[u]) {
if (v == fa) continue;
dp[v] = dp[u] - cnt[v] * w + (sum - cnt[v]) * w; // 先处理过去 / 回来的牛, 再向下递归
dfs2(v, u);
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> c[i];
sum += c[i];
}
for (int i = 1; i < n; ++i) {
int u, v, w;
cin >> u >> v >> w;
vec[u].push_back(make_pair(v, w));
vec[v].push_back(make_pair(u, w));
}
dfs1(1, 0);
dfs2(1, 0);
int ans = 1e9;
for (int i = 1; i <= n; ++i) {
ans = min(ans, dp[i]);
}
cout << ans + dis[1] << endl; // 答案要加上最初的距离
return 0;
}
树的直径(DP 解法)
设当
树形 DP 可以在存在负权边的情况下求解出树的直径(也可以边权加定值,用 4 次 DFS,两次求直径、两次求节点数)。
#include <iostream>
#include <vector>
using namespace std;
int n, d = 0;
int d1[10005], d2[10005];
vector<int> vec[10005];
static inline void dfs(int u, int fa) {
d1[u] = d2[u] = 0;
for (int v : vec[u]) {
if (v == fa) continue;
dfs(v, u);
int t = d1[v] + 1;
if (t > d1[u])
d2[u] = d1[u], d1[u] = t;
else if (t > d2[u])
d2[u] = t;
}
d = max(d, d1[u] + d2[u]);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
vec[u].push_back(v);
vec[v].push_back(u);
}
dfs(1, 0);
cout << d << endl;
return 0;
}
Refrences
[3.1] 树形 DP 求树的直径 - OI-Wiki
4. 背包 DP
01 背包 ^{4.1}
P2871 [USACO07DEC] Charm Bracelet S
题意:有
设
考虑转移。假设当前已经处理好了前
所以
考虑滚动数组优化
#include <iostream>
using namespace std;
int n, m;
int w[3500];
int d[3500];
int dp[12880];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> w[i] >> d[i];
}
for (int i = 1; i <= n; ++i) { // 枚举顺序不能错
for (int j = m; j >= w[i]; --j) { // 倒叙枚举不能错
dp[j] = max(dp[j], dp[j - w[i]] + d[i]);
}
}
cout << dp[m] << endl;
return 0;
}
P8803 [蓝桥杯 2022 国 B] 费用报销
题意:有
设
本题有些特殊(带限制),
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
int n, m, k, ans;
pair<int, int> a[1005];
int f[1005][5005];
int g[1005];
const int days[]{0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
static inline int date2day(int month, int day) { return days[month] + day; }
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= n; ++i) {
int mm, d;
cin >> mm >> d >> a[i].first;
a[i].second = date2day(mm, d);
}
sort(a + 1, a + n + 1, [](const pair<int, int> &x, const pair<int, int> &y) { return x.second < y.second; });
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
if (a[i].second - a[j].second >= k) {
g[i] = j;
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = m; j >= a[i].first; --j) {
f[i][j] = f[i - 1][j];
f[i][j] = max(f[i][j], f[g[i]][j - a[i].first] + a[i].first);
}
}
cout << f[n][m] << endl;
return 0;
}
完全背包
Refrences
[4.1] 背包 DP - OI Wiki
例题
P9743 「KDOI-06-J」旅行
题意:略。
设
容易写出这样的方程(记
未完待续
版权声明
本文使用 GPL 协议,原文发布在 GitHub,以下是本文的版权声明,转载请依然使用 GPL 协议。
Dynamic Programming Notes 动态规划笔记
Copyright (C) 2023 王一乐
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.