当前位置:网站首页>为什么完全背包要用顺序遍历?简要解释一下
为什么完全背包要用顺序遍历?简要解释一下
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种物品放入了多次。
所以完全背包应该用顺序遍历。
边栏推荐
- 每年 2000 亿投资进入芯片领域,「中国芯」创投正蓬勃
- [communication] optimal power allocation in the uplink of two-layer wireless femtocell network with matlab code
- Use mitmproxy to cache 360 degree panoramic web pages offline
- Design of short chain
- Microsoft win11 is still "unsatisfactory". Multi user feedback will cause frequent MSI crashes
- 【无人机】多无人协同任务分配程序平台含Matlab代码
- One minute to learn how to install the system, win7 XP, win10 and win11 become very simple
- Entropy information entropy cross entropy
- 氢创未来 产业加速 | 2022氢能专精特新创业大赛报名通道开启!
- 传统企业要为 Web3 和去中心化做的 11 个准备
猜你喜欢
The largest single investment in the history of Dachen was IPO today
Coscon'22 community convening order is coming! Open the world, invite all communities to embrace open source and open a new world~
koa2对Json数组增删改查
Experiment 4: installing packages from Gui
内网穿透zerotier 外网(手机、电脑等)访问内网设备(树莓派、NAS、电脑等)
Gold three silver four, don't change jobs
leetcode:236. 二叉树的最近公共祖先
若依请求url中带有jsessionid的解决办法
Stop saying that microservices can solve all problems
Ajout, suppression et modification d'un tableau json par JS
随机推荐
士大夫哈哈哈
Experiment 4: installing packages from Gui
The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
flinksql select id ,count(*) from a group by id .
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
同一个作业有两个source,同一链接不同数据库账号,为何第二个链接查出来的数据库列表是第一个账号的
Can online reload system software be used safely? Test use experience to share with you
The programmer refused the offer because of low salary, HR became angry and netizens exploded
Example code of MySQL split string as query condition
Summary of three methods for MySQL to view table structure
How to find out if the U disk file of the computer reinstallation system is hidden
Is the more additives in food, the less safe it is?
The best sister won the big factory offer of 8 test posts at one go, which made me very proud
Microsoft win11 is still "unsatisfactory". Multi user feedback will cause frequent MSI crashes
Gold three silver four, don't change jobs
若依请求url中带有jsessionid的解决办法
每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报...
js對JSON數組的增删改查
Hydrogen future industry accelerates | the registration channel of 2022 hydrogen energy specialty special new entrepreneurship competition is opened!
What should I do if the USB flash disk data is formatted and how can I recover the formatted USB flash disk data?