AcWing 3. 完全背包问题

朴素
#include<iostream>
#include<algorithm>
using namespace std ;
const int N=;
int n,m;
int v[N],w[N];
int f[N][N];
int main() {
cin>>n>>m;//n个物品 最大体积位m
for(int i=; i<=n; i++) cin>>v[i]>>w[i];
for(int i=; i<=n; i++)
for(int j=; j<=m; j++)
for(int k=; k*v[i]<=j; k++)//选k个第i个物品
f[i][j]=max(f[i][j],f[i-][j-v[i]*k]+k*w[i]);
cout<<f[n][m]<<endl;
return ;
}
优化二维
//01背包从i-1转移过来 而完全背包是从第
i层转移过来
#include <iostream>
#include <algorithm>
using namespace std;
const int N = ;
int n, m;
int v[N], w[N];
int f[N][N];
int main() {
cin >> n >> m;
for (int i = ; i <= n; i ++ ) cin >> v[i] >> w[i];
for (int i = ; i <= n; i ++ )
for (int j = ; j <= m; j ++ )
for(int k=; k*v[i]<=j; k++)
f[i][j] = max(f[i][j], f[i-][j - v[i]*k] + w[i]*k);
cout << f[n][m] << endl;
return ;
}
终极一维
//一维
#include <iostream>
#include <algorithm>
using namespace std;
const int N = ;
int n, m;
int v[N], w[N];
int f[N];
int main() {
cin >> n >> m;//n是数量,m是体积
for (int i = ; i <= n; i ++ )
cin >> v[i] >> w[i];
for (int i = ; i <= n; i ++ )
for (int j = v[i]; j <= m; j ++ )
f[j] = max(f[j], f[j - v[i]] + w[i]);
cout << f[m] << endl;
return ;
}
AcWing 3. 完全背包问题的更多相关文章
- AcWing 6. 多重背包问题 III
//f[i,j] 所有只从前i块能量石中选,且总体积恰好为j的方案数 #include <iostream> #include <algorithm> #include < ...
- AcWing 7. 混合背包问题
#include<iostream> #include<algorithm> #include<cstring> using namespace std ; ; i ...
- AcWing 9. 分组背包问题
#include <iostream> #include <algorithm> using namespace std; ; int n, m; int v[N][N], w ...
- AcWing 5. 多重背包问题 II
//二进制优化 最后变为01背包 #include <iostream> #include <algorithm> using namespace std; , M = ; i ...
- AcWing 4. 多重背包问题
朴素 数据范围小 //数据范围小 #include<iostream> #include<algorithm> using namespace std ; ; int n,m; ...
- AcWing 2. 01背包问题
朴素 //朴素二维 #include <iostream> #include <algorithm> using namespace std; ; int n, m; int ...
- acwing 4 多重背包问题 I
多重背包 有 n种物品 一共有 m大小的背包,每种物品的价值 大小 个数 为 s[i],v[i],num[i]; #include<bits/stdc++.h>//cmhao #defin ...
- acwing 2 零一背包问题
地址 https://www.acwing.com/problem/content/description/2/ 题目描述有 N 件物品和一个容量是 V 的背包.每件物品只能使用一次. 第 i 件物品 ...
- AcWing 11. 背包问题求方案数
//g[i,j]表示f[i,j]取最大值的方案数目 //体积最多是j 全部为0,v>=0 //体积恰好为j f[0][0]=0,f[i]=无穷,v>=0 //体积至少是j f[0][0]= ...
随机推荐
- JS 自动关闭页面
<script language=javascript> this.window.opener = null; window.close(); </script>
- Math Magic ZOJ - 3662
核心是要想到只枚举最小公倍数的因子 因为转移过程中一单添加了不是最小公倍数的因子,那么结果必然不合法,虽然最终答案是对的,但是这样的答案根本用不上,反而时间复杂度大大增加 #include<cs ...
- Java设计模式(工厂模式)
一.简单工厂模式 简单工厂模式就是把对类的创建初始化全都交给一个工厂来执行,而用户不需要去关心创建的过程是什么样的,只用告诉工厂我想要什么就行了.而这种方法的缺点也很明显,违背了设计模式的开闭原则,因 ...
- react-native-----hello word!
react-native运行helloword 今天是个特殊的时刻,我前天开始学习react-native,一直环境塔建出错,运行打包出错,今晚,我终于把这个难搞的环境给搭建好了,并成功运行了第一个h ...
- 获取redis cluster master对应的slot分布情况
需求:原生的redis-trib.rb功能是强大,但输出的内容过于繁杂,比如我需要关注哪些master对应哪些slots,不是很直观,如果集群的规模更大的话,那么输出的结果获取信息更加困难. 说明:这 ...
- babel plugin
a = () => {}, // Support for the experimental syntax 'classProperties' isn't currently enabled ya ...
- IP地址分类及其相关计算问题
IP地址分类及其相关计算问题 公网IP和子网IP 公网IP: • A类:1.0.0.0 到 127.255.255.255 主要分配 给大量主机而局域网网络数量较少的大型网络 • B类:128.0.0 ...
- TODO:rds数据库实例
rds数据库实例怎么创建的 rds数据库实例高可用是怎么实现的 rds备份是怎么实现的 参考: https://www.cnblogs.com/jackyzzy/p/7384355.html http ...
- 【转载】深入理解Java虚拟机笔记---运行时栈帧结构
栈帧(Stack Frame)是用于支持虚拟机进行方法调用和方法执行的数据结构,它是虚拟机运行时数据区的虚拟机栈(Virtual Machine Stack)的栈元素.栈帧存储了方法的局部变量表,操作 ...
- AdaBoost级联分类器
Haar分类器使用AdaBoost算法,但是把它组织为筛选式的级联分类器,每个节点是多个树构成的分类器,且每个节点的正确识别率很高.在任一级计算中,一旦获得“不在类别中”的结论,则计算终止.只有通过分 ...