背包问题
SYaoJun
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
2443 分成两组【51nod】
题目链接 类似的题目 正整数分组 解题思路 直接搜索:选与不选 时间复杂度O(2n)O(2^n)O(2n)由于n=20n=20n=20所以时间在10610^6106左右 #include <iostream> #include <algorithm> #include <cstring> using namespace std; const int N = 22; int a[N]; typedef long long LL; LL n, sum = 0, a原创 2021-06-08 21:10:37 · 482 阅读 · 0 评论 -
乔乔的包【多重背包】
版本1 完全背包版本 #include <iostream> #include <algorithm> #include <cstring> #include <vector> using namespace std; const int N = 2048; int mm[N], w[N], s[N]; int dp[N][N]; int main(){ int n, m; scanf("%d %d", &n, &m); for(in原创 2021-06-03 11:32:02 · 165 阅读 · 0 评论 -
整数求和【01背包】
版本1 题目链接 转移方程 dp[i][j] = dp[i-1][j] + dp[i-1][j-i] 初始化 值为0时,有一种方案 #include <iostream> #include <algorithm> #include <cstring> #include <vector> using namespace std; const int N = 128; int a[N]; int dp[N][N]; int main(){原创 2021-06-03 11:04:22 · 246 阅读 · 0 评论 -
最小邮票数【清华大学】
01背包 #include <cstdio> #include <iostream> #include <cstring> #include <vector> using namespace std; const int N = 32; const int M = 128; const int MAX = 0x3f3f3f3f; int w[N]; int main(){ int m, n; scanf("%d %原创 2021-05-28 15:13:07 · 148 阅读 · 0 评论 -
外卖满减
公司:美团 类型:01背包 题目 题意:大于等于x的最小数 //外卖满减 #include <bits/stdc++.h> using namespace std; const int N = 103; int p[N]; int dp[N*N]; int main(){ //01背包问题 int n, x; scanf("%d%d", &n, &x); in...原创 2020-03-26 20:23:11 · 515 阅读 · 0 评论 -
考试策略
公司:美团 类型:01背包 题目 题意:01背包模板题 #include <bits/stdc++.h> using namespace std; const int N = 123; int p[N], a[N], q[N], b[N]; int dp[N]; int main(){ //01背包问题 int n; scanf("%d", &n); for(int ...原创 2020-03-26 20:20:04 · 366 阅读 · 0 评论 -
01背包【B站正月点灯笼讲解版和yxc版】
核心代码 使用滚动数组将二维的数据优化为一维,降低了空间复杂度。 直接套模板 //物品数量是5 最大容量是 20 int d[21]={0}; void knapsack2(){ for(int i = 1; i <= 5; i++){ for(int c = 20; c >= w[i]; c--){ d[c] = max(d[c], d[c - w[i]] + v[i...原创 2019-08-21 08:35:48 · 273 阅读 · 0 评论 -
完全背包
核心代码 一维滚动数组正向枚举,而01背包必须时逆向枚举。 //物品数量是5 最大容量是 20 void knapsack2(){ for(int i = 1; i <= 5; i++){ for(int c = w[i]; c <= 20; c++){ //注意从自减改成自增,其他不用变 d[c] = max(d[c], d[c - w[i]] + v[i]); }...原创 2019-08-21 09:00:11 · 266 阅读 · 0 评论 -
01背包问题【AcWing闫雪灿讲解版】
AcWing题目链接 #include<iostream> using namespace std; const int maxn = 1010; int w[maxn], v[maxn]; int dp[maxn] = {0}; int main(){ int N, V; cin>> N >> V; for(int i = 1; i <= N; i...原创 2019-08-22 07:19:23 · 811 阅读 · 0 评论 -
完全背包问题
AcWing题目链接 #include<iostream> using namespace std; const int maxn = 1010; int w[maxn], v[maxn]; int dp[maxn] = {0}; int main(){ int N, V; cin>> N >> V; for(int i = 1; i <= N; i...原创 2019-08-22 07:21:34 · 209 阅读 · 0 评论 -
51nod 1007 正整数分组
题目链接 简单的背包问题 时间复杂度O(n2)O(n^2)O(n2) 如果用递归的话时间复杂度是O(2n)O(2^n)O(2n) #include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <cstring> using nam...原创 2019-09-02 11:15:21 · 112 阅读 · 0 评论 -
Bone Collector【HDOJ2602】
01背包问题#include<cstdio> #include<cstring> int max(int a,int b){ return a>b?a:b; } int main(){ int T; int w[1005],c[1005]; scanf("%d",&T); for(int i=0;i<T;i++){ memset(w,0,siz原创 2018-01-08 14:51:39 · 312 阅读 · 0 评论 -
最大报销额【浙江大学】★
题目链接 解题思路 首先把满足题目要求的发票保存在向量中,然后用深搜求最优解,其实也可以用01背包! #include<bits/stdc++.h> using namespace std; double ans, total; vector<double> v; void dfs(int i, int t, int sumV){ if(i==t) return; df...原创 2019-08-02 10:19:55 · 563 阅读 · 0 评论 -
Sum of Factorials【上海交通大学】
题目链接 按照01背包的思路,由于数据量较小,直接用深搜搞! #include<bits/stdc++.h> using namespace std; int A[10]={1,1,2,6,24,120,720,5040,40320,362880}; int n,ans; void dfs(int index,int sum){ if(index==10) return; dfs...原创 2019-08-01 18:50:56 · 324 阅读 · 0 评论
分享