
动态规划
开学了8
go on
展开
-
3417. 砝码称重
dp[i][j]; // dp[i][j] 表示前i个砝码,能表示j克的重量吗? // 初始状态: dp[0][0] = 1;// 转台转移: dp[i][j] = dp[i-1][j] || dp[i-1][a[i]+j] || dp[i-1][abs(a[i]-j)];表示不放第 i 个砝码,放第 i 个砝码在左边,放第 i 个砝码在右边#include <iostream>#include <cstring>#include <algorithm>原创 2022-03-28 21:32:54 · 230 阅读 · 0 评论 -
AcWing 187. 导弹防御系统 (最长上升子序列+dfs 求最小值的两种方法)
定义全局变量求最小值#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int N = 55;int n,ans,a[N],up[N],down[N];void dfs(int u, int su, int sd){ if(su + sd >= ans) return ; if(u == n) { ans = su原创 2021-09-29 16:35:48 · 91 阅读 · 0 评论 -
AcWing 1010. 拦截导弹 (最长上升子序列+Dilworth定理)
Dilworth 定理https://www.acwing.com/solution/content/10173/问一个序列最少用多少不上升序列能恰好覆盖Dilworth定理:对于一个偏序集,最少链划分等于最长反链长度。#include <iostream>#include <cstring>#include <algorithm>using namespace std;int n, t, a[1010],dp[1010],f[1010],ans,cn原创 2021-09-29 15:26:46 · 105 阅读 · 0 评论 -
最大上升子序列(模板题)
#include <iostream>#include <cstring>#include <algorithm>using namespace std;int main(){ int n; cin >> n; int a[n+10], f[n+10]; for(int i = 0; i < n; i++) cin >> a[i], f[i] = a[i]; for(int i原创 2021-09-29 09:51:27 · 78 阅读 · 0 评论 -
AcWing 1012. 友好城市 (搞心态 + 最长上升子序列)
最开始求 a 数组的时候,太搞人心态了细想,细想#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int N = 5e3 + 100;int a[N],c[N],f[N];struct Node{ int x, y; bool operator < (const Node &A) const原创 2021-09-28 23:29:44 · 98 阅读 · 0 评论 -
482. 合唱队形 (最长上升子序列+思维)
还是最长上升子序列的三种运用方式#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int N = 110;int a[N], f1[N], f2[N], f3[N]; int main(){ int n; cin >> n; int ans = 1; for (int i = 0; i &l原创 2021-09-28 22:14:02 · 94 阅读 · 0 评论 -
AcWing 1014. 登山 (最长上升子序列 + 思维)
最长上升子序列分为三种情况去讨论#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int N = 1e3 + 100;int a[N], f[N], dp[N], ff[N];int main(){ int n, ans = 1; cin >> n; for (int i = 0; i < n原创 2021-09-28 22:00:32 · 79 阅读 · 0 评论 -
怪盗基德的滑翔翼(最长上升子序列)
#include <iostream>#include <cstring>#include <algorithm>using namespace std;int n;int a[110], f[110],dp[110];int main(){ int t; cin >> t; while(t--) { cin >> n; int ans = 1; fo原创 2021-09-28 20:50:33 · 106 阅读 · 0 评论 -
AcWing 1027. 方格取数(高难度线性dp)
本来想着先用dp获取最大值,然后标记,最后再dp一遍,貌似可以实现。正解:两条路线每次走的步数是一样的,k = i1 + j1 = i2 + j2比较来自四个方位的点的大小引用的目的是为了让 dp 随之改变#include <iostream>#include <cstring>#include <algorithm>using namespace std;int v[15][15];int dp[50][15][15];int main(){原创 2021-09-27 22:25:27 · 99 阅读 · 0 评论 -
1018. 最低通行费 (线性dp 最小值注意边界的优化)
边界不优化,min的时候取到0#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int inf = 0x3f3f3f3f;int a[110][110], dp[110][110];int main(){ int n; cin >> n; memset(dp, 0x3f, sizeof dp);原创 2021-09-27 15:41:06 · 134 阅读 · 0 评论 -
AcWing 1015. 摘花生(线性dp)
#include <iostream>#include <cstring>#include <algorithm>using namespace std;int dp[110][110];int a[110][110];int main(){ int t; cin >> t; while(t--) { memset(dp,0,sizeof dp); int n, m;原创 2021-09-27 15:18:47 · 216 阅读 · 0 评论 -
动态规划+字符串哈希判断有无冲突
https://codeforces.com/group/vFwRVj9WjO/contest/345181/problem/H第一次交的时候p用的是1e7,有冲突,用13331可以。题解:判断前1-5个是否出现过,如果出现了,计算它的贡献值。#include <iostream>#include <cstring>using namespace std;#include <map>const int N = 1e5 + 10;const int mod原创 2021-09-20 16:38:06 · 115 阅读 · 0 评论 -
石子合并问题(区间dp)
#include <iostream>using namespace std;#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)const int N = 310; // O(n^3)int n;int s[N], a[N], f[N][N];void solve(){ memset(f, 0x3f, sizeof(f)); cin >> n; for原创 2021-06-29 16:28:22 · 86 阅读 · 0 评论