随笔分类 -  dp

摘要:链接 : C. Moamen and XOR 题意 : 给定 \(n\) 个数和 \(k\), 保证每个数 \(a_i\lt 2^k\), 问使得 \(a_1 \ \&\ a_2\ \&\ a_3\ \&\ ...\ \&\ a_n \ge a_1 \oplus a_2 \oplus a_3 \op 阅读全文
posted @ 2021-08-10 18:54 phr2000 阅读(102) 评论(0) 推荐(0)
摘要:[D. Make The Fence Great Again](Problem - 1221D - Codeforces) 思路 每个数最多加两次, 想明白这个直接 dp 就可以了. \(f[i][j]:表示前i个满足题意并且第i个数加j次的最小花销.\) #include <bits/stdc++ 阅读全文
posted @ 2021-05-25 18:55 phr2000 阅读(71) 评论(0) 推荐(0)
摘要:C. Valera and Elections dp + dfs 题意 一棵树, 树边有的有标记有的没标记, 如果选择一个点, 能将点到根最短路径上的边全部打上标记, 问最少选几个点, 使所有的边都被打上标记. 思路 \(f[i]:表示最少需要选择的点数使以i为根的子树都被标记.\) 因此如果u的邻 阅读全文
posted @ 2021-05-25 17:17 phr2000 阅读(53) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1015/ 由dp输出方案 分组背包 #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie 阅读全文
posted @ 2021-04-13 20:31 phr2000 阅读(46) 评论(0) 推荐(0)
摘要:f[i][j]: 表示氧气至少为i,氮气至少为j至少需要的重量. f[j][k] = min(f[j][k], f[max(0, j - a)][max(0, k - b)] + w); 由实际意义出发 : 如果a > j或b > k也是需要转移过来的. 如果这里不转移, f[i][j]的意义就变了 阅读全文
posted @ 2021-04-13 19:54 phr2000 阅读(51) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/7/ 混合背包 多重背包用二进制优化. #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.ti 阅读全文
posted @ 2021-04-13 19:15 phr2000 阅读(65) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/280/ \(完全背包变式\) 01背包变式 #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin 阅读全文
posted @ 2021-04-12 21:41 phr2000 阅读(39) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/280/ \(01背包变式\) \(f[i][j]:表示在前i个中选,选出的数和为j的个数.\) for (int i = 1; i <= n; ++i) f[i][0] = 1; for (int i = 1; i <= 阅读全文
posted @ 2021-04-12 21:21 phr2000 阅读(109) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1024/ 二维01背包 #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); c 阅读全文
posted @ 2021-04-12 19:54 phr2000 阅读(55) 评论(0) 推荐(0)
摘要:http://codeforces.com/contest/1513/problem/C dp $f[i][j]:$表示当前数位上为i(0~9), 加法次数为j最终形成的数位. f[i][j] = f[i - 1][j + 1]; f[9][j] = f[1][j + 1] + f[0][j + 1 阅读全文
posted @ 2021-04-12 18:37 phr2000 阅读(155) 评论(1) 推荐(0)
摘要:导弹防御系统 本题就是拦截导弹的dfs版 能覆盖整个序列的最少的不上升子序列的个数等价于该序列的最长上升子序列长度 能覆盖整个序列的最少的不下降子序列的个数等价于该序列的最长下降子序列长度 #include <bits/stdc++.h> using namespace std; #define I 阅读全文
posted @ 2021-04-11 21:51 phr2000 阅读(184) 评论(0) 推荐(0)
摘要:拦截导弹 题意: 最少用几个不上升子序列完全覆盖原序列. 贪心: 处理一个不下降队列, 队列中的每个数都表示我们需要求得的不上升子序列的末尾.那么最终队列有多少个数,就表示答案是多少. 为了处理出这个队列q,我们遍历原数组s,对于每一个x: 若x大于等于q中每一个数,把x放到q的末尾 否则,找到q中 阅读全文
posted @ 2021-04-10 10:55 phr2000 阅读(51) 评论(0) 推荐(0)
摘要:传纸条 这道题要注意的是横坐标取值范围: x + y = k => y = k - x => 1 <= y = k - x <= m => k - m <= x <= k - 1 for (int k = 2; k <= m + n; ++k) for (int x1 = max(1, k - m) 阅读全文
posted @ 2021-04-08 23:01 phr2000 阅读(92) 评论(0) 推荐(0)
摘要:方格取数 令k为横纵坐标之和, 可以省去一维. 这两遍走法是互相无影响的. 如果会同时走上同一个坐标, 这个坐标的值就只加一次就好了, 这样就表示了第二走的值为0 #include <bits/stdc++.h> using namespace std; #define IO ios::sync_w 阅读全文
posted @ 2021-04-08 21:28 phr2000 阅读(113) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1093/ \(有一个 a×b 的整数组成的矩阵,现请你从中找出一个 n×n 的正方形区域,使得该区域所有数中的最大值和最小值的差最小。\) 思路 \(分别得到每个n*n正方形中的最小值和最大值\) \(对于每一行做一遍单 阅读全文
posted @ 2021-02-04 23:29 phr2000 阅读(81) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/description/1089/ 思路 \(单调对列\ + \ dp\) \(不能选超过m个连续的数求最大等价于在每m+1个数中选出最小的一个.\) \(单调队列中维护的f[i-1,\ i-m-1]区间的最小值\) \( 阅读全文
posted @ 2021-02-04 20:55 phr2000 阅读(68) 评论(0) 推荐(0)
摘要:#include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) inline int lowbit(int x) { return x & (-x 阅读全文
posted @ 2021-02-04 19:41 phr2000 阅读(51) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1092/ \(二分\ + \ 单调队列dp\) #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);c 阅读全文
posted @ 2021-02-04 01:58 phr2000 阅读(148) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1091/ 状态表示 \(f[i]:点燃第i个且满足且每m个至少点燃一个的全体集合\) \(属性:min\) 状态转移 $f[i] = f[q[hh]]+f[i]\$ #include <bits/stdc++.h> us 阅读全文
posted @ 2021-02-04 01:56 phr2000 阅读(64) 评论(0) 推荐(0)
摘要:https://www.acwing.com/activity/content/problem/content/1458/1/ \(用单调队列得到[i-m,\ i-1]区间中最小的是s[j],\ s[i]-s[j]即为所求\) #include <bits/stdc++.h> using names 阅读全文
posted @ 2021-02-04 01:51 phr2000 阅读(44) 评论(0) 推荐(0)