当前位置:网站首页>为什么完全背包要用顺序遍历?简要解释一下
为什么完全背包要用顺序遍历?简要解释一下
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种物品放入了多次。
所以完全背包应该用顺序遍历。
边栏推荐
- Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
- Oracle对表进行的常用修改命令
- The best sister won the big factory offer of 8 test posts at one go, which made me very proud
- The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
- 不要再说微服务可以解决一切问题了
- matplotlib画柱状图并添加数值到图中
- [system analyst's road] Chapter 7 double disk system design (service-oriented development method)
- Laravel8 uses passport authentication to log in and generate a token
- Can async i/o be implemented by UDF operator and then called by SQL API? At present, it seems that only datastre can be seen
- 电脑重装系统u盘文件被隐藏要怎么找出来
猜你喜欢

《数字经济全景白皮书》保险数字化篇 重磅发布

matplotlib画柱状图并添加数值到图中

How to implement Lua entry of API gateway

Résumé des connaissances de gradle

The problem of ASP reading Oracle Database

Please help xampp to do sqlilab is a black

(1)长安链学习笔记-启动长安链

The largest single investment in the history of Dachen was IPO today

B 站弹幕 protobuf 协议还原分析
![[unmanned aerial vehicle] multi unmanned cooperative task allocation program platform, including Matlab code](/img/4c/5d867437aac5faa299817e187602e1.png)
[unmanned aerial vehicle] multi unmanned cooperative task allocation program platform, including Matlab code
随机推荐
短链的设计
I've been laid off, and I'll lose money for everything. The days when I once made a monthly salary of 20000 are not coming back
Today, I met a senior test developer from Tencent and saw the ceiling of the foundation
The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
Competition between public and private chains in data privacy and throughput
AI表现越差,获得奖金越高?纽约大学博士拿出百万重金,悬赏让大模型表现差劲的任务...
零代码高回报,如何用40套模板,能满足工作中95%的报表需求
Experiment 6: installing eve-ng
Master binary tree in one article
Eureka Client启动后就关闭 Unregistering application xxx with eureka with status DOWN
传统企业要为 Web3 和去中心化做的 11 个准备
What should I do if the USB flash disk data is formatted and how can I recover the formatted USB flash disk data?
How to find out if the U disk file of the computer reinstallation system is hidden
Yaduo Sangu IPO
Should the jar package of MySQL CDC be placed in different places in the Flink running mode?
flinksql select id ,count(*) from a group by id .
Efficient ETL Testing
Automatically update selenium driver chromedriver
MySQL数据库之JDBC编程
Spark Tuning (II): UDF reduces joins and judgments