当前位置:网站首页>2.01 backpack problem
2.01 backpack problem
2022-06-28 13:20:00 【HHppGo】
subject :
Yes N Items and a capacity are V The backpack . Each item can only be used once .
The first i The volume of the item is vi, The value is wi.
Find out which items to pack in your backpack , The total volume of these items can not exceed the capacity of the backpack , And the total value is the greatest .
Output maximum value .
Input format :
The first line has two integers ,N,V, Space off , They are the number of items and the volume of the backpack .
Next there is N That's ok , Two integers per line vi,wi, Space off , Separate indication control i The volume and value of items .
Output format :
Output an integer , Represents the greatest value .
Data range :

sample input :
4 5
1 2
2 4
3 4
4 5
sample output :
8
Ideas :

Solution one code :
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1100;
int n,m;
int f[N][N],v[N],w[N];
int main()
{
cin>>n>>m;
for(int i = 1;i<=n;i++)cin>>v[i]>>w[i];
for(int i = 1;i<=n;i++)
for(int j= 0;j<=m;j++)
{
f[i][j] = f[i-1][j];
if(j >= v[i] ) f[i][j] = max(f[i][j],f[i-1][j-v[i]] + w[i]);
}
cout<<f[n][m]<<endl;
return 0;
}
Solution II code :( Optimized one-dimensional approach )
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1100;
int n,m;
int f[N],v[N],w[N];
int main()
{
cin>>n>>m;
for(int i = 1;i<=n;i++)cin>>v[i]>>w[i];
for(int i = 1;i<=n;i++)
for(int j=m;j>=v[i];j--)
{
f[j] = max(f[j],f[j-v[i]] + w[i]);
}
cout<<f[m]<<endl;
return 0;
}
边栏推荐
- 完全背包 初学篇「建议收藏」
- Hubble数据库x某股份制商业银行:冠字号码管理系统升级,让每一张人民币都有 “身份证”
- 全志V853芯片 如何在Tina V85x平台切换sensor?
- Electronic components distribution 1billion Club [easy to understand]
- SHAREit实力出众,登陆全球 IAP 实力榜 Top7
- Here comes Wi Fi 7. How strong is it?
- codeblocks mingw安装配置问题
- 开源项目维权成功案例: Spug 开源运维平台成功维权
- scratch旅行相册 电子学会图形化编程scratch等级考试一级真题和答案解析2022年6月
- 电脑无线网络不显示网络列表应该如何解决
猜你喜欢

在线JSON转PlainText工具

投资98万美元的Saas项目失败了

Embedded development: seven techniques for estimating battery life

Hubble database x a joint-stock commercial bank: upgrade the number management system of Guanzi, so that every RMB has an "ID card"

Scratch travel photo album Electronic Society graphical programming scratch grade examination level 1 true questions and answers analysis June 2022
![[today in history] June 28: musk was born; Microsoft launches office 365; The inventor of Chua's circuit was born](/img/bf/09ccf36caec099098a22f0e8b670bd.png)
[today in history] June 28: musk was born; Microsoft launches office 365; The inventor of Chua's circuit was born
![BUUCTF:[WUSTCTF2020]朴实无华](/img/0f/a7973d3f7593f2464e48609e27d7bd.png)
BUUCTF:[WUSTCTF2020]朴实无华

新品体验:阿里云新一代本地SSD实例i4开放公测

SHAREit實力出眾,登陸全球 IAP 實力榜 Top7

Vscode如何设置自动保存代码
随机推荐
[cloud native] can self-service reports and Bi do so many things?
The press conference of Tencent cloud Database & CSDN engineer's ability lightweight certification is coming
2. 01背包问题
Which company has a low rate for opening a securities account? How to open an account is the safest
How about stock online account opening and account opening process? Is it safe to open a mobile account?
基于SSM实现水果蔬菜商城管理系统
JS class is not just a simple syntax sugar!
RK3399平台开发系列讲解(使用篇)Pinctrl子系统的介绍 - 视频介绍
Fastposter v2.8.4 release e-commerce poster generator
thinkphp6 多级控制器目录访问解决方法
Centos7: switch MySQL users and log in to MySQL
The English translation of heartless sword Zhu Xi's two impressions of reading
数启扬帆,智聚人才 | 腾讯云数据库 & CSDN 工程师能力轻量认证发布会重磅来袭!...
mysql数据库扫盲,你真的知道什么是数据库嘛
List集合转数组
Tencent tangdaosheng: facing the new world of digital and real integration, developers are the most important "architects"
China Database Technology Conference (DTCC) specially invited experts from Kelan sundb database to share
Vscode shortcut key
[MySQL from introduction to mastery] [advanced part] (III) creation of MySQL users_ Modification_ Delete and password settings
2022年中国运维安全产品市场规模及发展趋势预测分析