当前位置:网站首页>为什么完全背包要用顺序遍历?简要解释一下
为什么完全背包要用顺序遍历?简要解释一下
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种物品放入了多次。
所以完全背包应该用顺序遍历。
边栏推荐
- Gradle knowledge generalization
- Résumé des connaissances de gradle
- Isomorphism + cross end, knowing applet +kbone+finclip is enough!
- 若依请求url中带有jsessionid的解决办法
- flinksql select id ,count(*) from a group by id .
- 【无人机】多无人协同任务分配程序平台含Matlab代码
- 零代码高回报,如何用40套模板,能满足工作中95%的报表需求
- Today, I met a senior test developer from Tencent and saw the ceiling of the foundation
- 《数字经济全景白皮书》保险数字化篇 重磅发布
- Knowledge * review
猜你喜欢

亚朵三顾 IPO

Why is bat still addicted to 996 when the four-day working system is being tried out in Britain?

(1) Chang'an chain learning notes - start Chang'an chain

人均瑞数系列,瑞数 4 代 JS 逆向分析

Stop saying that microservices can solve all problems

若依请求url中带有jsessionid的解决办法
![[communication] optimal power allocation in the uplink of two-layer wireless femtocell network with matlab code](/img/47/741b89d94a2b0003937f32bdedfa19.png)
[communication] optimal power allocation in the uplink of two-layer wireless femtocell network with matlab code

How much does the mlperf list weigh when AI is named?

Competition between public and private chains in data privacy and throughput

快手的新生意,还得靠辛巴吆喝?
随机推荐
Wu Enda 2022 machine learning course evaluation is coming!
Master binary tree in one article
快手的新生意,还得靠辛巴吆喝?
js對JSON數組的增删改查
公链与私链在数据隐私和吞吐量上的竞争
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
编译logisim
《数字经济全景白皮书》保险数字化篇 重磅发布
11 preparations for Web3 and Decentralization for traditional enterprises
Yaduo Sangu IPO
The intranet penetrates the zerotier extranet (mobile phone, computer, etc.) to access intranet devices (raspberry pie, NAS, computer, etc.)
Hydrogen future industry accelerates | the registration channel of 2022 hydrogen energy specialty special new entrepreneurship competition is opened!
Gpt-3 is a peer review online when it has been submitted for its own research
Server SMP, NUMA, MPP system learning notes.
js导入excel&导出excel
Entropy information entropy cross entropy
Daily question brushing record (XV)
The important data in the computer was accidentally deleted by mistake, which can be quickly retrieved by this method
Design a red envelope grabbing system
flinksql select id ,count(*) from a group by id .