随笔分类 - DP之路
1
摘要:有点像完全背包,但也不是因为有数量的限制,是01背包的变式,每个硬币选与不选,最后看看能不能达到x。 #include<bits/stdc++.h> using namespace std; int t[100],w[100],dp[10005]; int main(){ int n,m; cin>
阅读全文
摘要:经典的过河卒问题,dp状态很好想,只需注意#不能发生转移即可 #include<bits/stdc++.h> #define int long long using namespace std; const int N=1e3+10; const int mod=1e9+7; char mp[N][
阅读全文
摘要:每个点肯定是它上个点转移过来的 #include<bits/stdc++.h> using namespace std; const int N=1e5+10; vector<int>a[N]; int d[N],dp[N]; void solve(){ int n,m; cin>>n>>m; fo
阅读全文
摘要:这个题目的体积很大,但是价值却很小,最多是1e5,我们可以转变背包体积概念,把价值当作体积,然后体积当作 DP 值。 dp[i] 表示的是达到i价值所需的最小的体积 #include<bits/stdc++.h> #define int long long using namespace std;
阅读全文
摘要:用dp[i][j] 表示第i天选了j类型的最大值 #include<bits/stdc++.h> using namespace std; const int N=1e5+10; vector<int>a[N]; int dp[N][3]; void solve(){ int n; cin>>n;
阅读全文
摘要:因为k很小,所以无需优化dp #include<bits/stdc++.h> using namespace std; void solve(){ int n,k; cin>>n>>k; vector<int>a(n+1); vector<int>dp(n+1,1e18); dp[1]=0; for
阅读全文
摘要:很好想的线性p #include<bits/stdc++.h> using namespace std; void solve(){ int n; cin>>n; vector<int>a(n+1); vector<int>dp(n+1,1e18); dp[1]=0; for(int i=1;i<=
阅读全文
摘要:每个数字选与不选的01背包 本题的核心就是每个容量j,最多选t[i]个,然后不断递归 #include<bits/stdc++.h> using namespace std; const int N=105; const int M=1e5+10; int w[N],t[N]; int dp[M];
阅读全文
摘要:[NOIP2002 普及组] 过河卒 题目描述 棋盘上 \(A\) 点有一个过河卒,需要走到目标 \(B\) 点。卒行走的规则:可以向下、或者向右。同时在棋盘上 \(C\) 点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点。因此称之为“马拦过河卒”。 棋盘用坐标表示,\(A\)
阅读全文
摘要:最长上升子序列 题目描述 这是一个简单的动规板子题。 给出一个由 \(n(n\le 5000)\) 个不超过 \(10^6\) 的正整数组成的序列。请输出这个序列的最长上升子序列的长度。 最长上升子序列是指,从原序列中按顺序取出一些数字排在一起,这些数字是逐渐增大的。 输入格式 第一行,一个整数 \
阅读全文
摘要:其中1有点特殊所以就直接从2开始了 #include<bits/stdc++.h> using namespace std; int w[2000],f[2000]; int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ w[i]++; for(int
阅读全文
摘要:一个数可以被无限次的选,所以是完全背包,然后预处理一下就好啦 #include<bits/stdc++.h> using namespace std; const int N=1e5+10; int f[N]; int main(){ memset(f,0x3f,sizeof f); f[0]=0;
阅读全文
摘要:还是选与不选的问题,但是每个背包可以无限次选,所以这是个完全背包! #include<bits/stdc++.h> using namespace std; const int N=2e4+10; int f[N],w[N],t[N]; int main(){ int n,m; cin>>n>>m;
阅读全文
摘要:二维01背包的裸题 #include<bits/stdc++.h> using namespace std; int w[200],a[200],b[200]; int f[2000][2000]; int main(){ int n,x,y; cin>>n>>x>>y; for(int i=1;i
阅读全文
摘要:这个题目挺有意思的,有点贪心思想,就是要把更多的时间留给刷题,所以要把01背包改成取min,所以要把dp[i]先预处理成0x3f无穷大,然后把刷题时间排个序,这要就是最佳的答案。 #include<bits/stdc++.h> using namespace std; int a[20],b[20]
阅读全文
摘要:题目思路与解法都与 NASA的食物计划 https://www.luogu.com.cn/problem/P1507 类似 是二维01背包 #include<bits/stdc++.h> using namespace std; int f[500][500]; int a[110],b[110];
阅读全文
摘要:还是选与不选的问题并且只能选一次,所以是01背包,但是这个题目是个二维的01背包,因为它必须要满足两个条件,这个是满足体积的情况下,一个是满足质量的情况下 #include<bits/stdc++.h> using namespace std; const int N=500; int f[N][N
阅读全文
摘要:题目与P2639十分相似 #include<bits/stdc++.h> using namespace std; const int N=5e4+10; int f[N],t[5010]; int main(){ int T,n; cin>>T>>n; for(int i=1;i<=n;i++){
阅读全文
摘要:大概就是在不超过容量的情况下,问你最多能吃多少 是吃与不吃,选与不选的问题,所以是01背包,但是是变式 #include<bits/stdc++.h> using namespace std; const int N=5e4; int f[N],t[1000]; int main(){ int T,
阅读全文
摘要:所以这是一个01背包的裸题,每个物品选与不选 dp[i][j] 在前面i个物品选择,在不超过j的前提先所能选到的最大价值 公式就出来了 dp[i][j] = max(dp[i-1][j],dp[i-1][j-t[i]]+w[i]) 这是01背包的递推公式 注意的是,该公式还可以优化,因为第i个是从第
阅读全文
1