当前位置:网站首页>为什么完全背包要用顺序遍历?简要解释一下
为什么完全背包要用顺序遍历?简要解释一下
2022-07-06 16:16:00 【童 话】
完全背包的意思是每种物品可以多次放入背包。
dp[j] = max(dp[j], dp[j - weight[i]] + value[i])
dp[6] = max(dp[6], dp[6 - 1] + 2)
= max(dp[6], max(dp[5], dp[5 - 1] + 2) + 2)
dp[j]表示容量为j的背包装了物品后的最大价值。
1表示第i种物品的重量,2表示第i种物品的价值。
初始的dp[5]和dp[6]全为0。
这里,因为初始dp[6]是0,所以肯定选了dp[6 - 1] + 2,也就是肯定放入了第i种物品。
而dp[6]=……的执行要用到dp[5]的结果。
由于是顺序执行,所以dp[6]执行之前肯定已经执行了dp[5]=……。
dp[5]=……执行时,因为初始dp[5]是0,所以肯定选了dp[5 - 1] + 2,也就是肯定也放入了第i种物品。
所以说第i种物品放入了多次。
所以完全背包应该用顺序遍历。
边栏推荐
- How to implement Lua entry of API gateway
- Detailed explanation of regular expression (regexp) in MySQL
- Hydrogen future industry accelerates | the registration channel of 2022 hydrogen energy specialty special new entrepreneurship competition is opened!
- AI金榜题名时,MLPerf榜单的份量究竟有多重?
- 请问async i/o可以由udf算子实现然后用sql api调用吗?目前好像只看到Datastre
- Wasserstein Slim GAIN with Gradient Penalty(WSGAIN-GP)介绍及代码实现——基于生成对抗网络的缺失数据填补
- How can Oracle CDC deserialize with jsondebeziumdeserializationschema
- Experiment 5: common automation libraries
- Without CD, I'll teach you a trick to restore the factory settings of win10 system
- 这个『根据 op 值判断操作类型来自己组装 sql』是指在哪里实现?是指单纯用 Flink Tabl
猜你喜欢
The problem of ASP reading Oracle Database
Gradle知識概括
公链与私链在数据隐私和吞吐量上的竞争
人均瑞数系列,瑞数 4 代 JS 逆向分析
Ajout, suppression et modification d'un tableau json par JS
Use mitmproxy to cache 360 degree panoramic web pages offline
Résumé des connaissances de gradle
Up to 5million per person per year! Choose people instead of projects, focus on basic scientific research, and scientists dominate the "new cornerstone" funded by Tencent to start the application
浅谈现在的弊端与未来的发展
MATLIB从excel表中读取数据并画出函数图像
随机推荐
js對JSON數組的增删改查
Please help xampp to do sqlilab is a black
Restoration analysis of protobuf protocol of bullet screen in station B
【无人机】多无人协同任务分配程序平台含Matlab代码
Stop saying that microservices can solve all problems
Zero code and high return. How to use 40 sets of templates to meet 95% of the reporting needs in the work
[communication] optimal power allocation in the uplink of two-layer wireless femtocell network with matlab code
请问async i/o可以由udf算子实现然后用sql api调用吗?目前好像只看到Datastre
Summary of three methods for MySQL to view table structure
【212】php发送post请求有哪三种方法
谁说新消费品牌大溃败?背后有人赢麻了
MySQL数据库之JDBC编程
Design a red envelope grabbing system
Koa2 addition, deletion, modification and query of JSON array
Competition between public and private chains in data privacy and throughput
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
实现多彩线条摆出心形
浅谈现在的弊端与未来的发展
MySQL connected vscode successfully, but this error is reported
The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance