当前位置:网站首页>为什么完全背包要用顺序遍历?简要解释一下
为什么完全背包要用顺序遍历?简要解释一下
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种物品放入了多次。
所以完全背包应该用顺序遍历。
边栏推荐
- What does front-end processor mean? What is the main function? What is the difference with fortress machine?
- The programmer refused the offer because of low salary, HR became angry and netizens exploded
- Hydrogen future industry accelerates | the registration channel of 2022 hydrogen energy specialty special new entrepreneurship competition is opened!
- Basic chart interpretation of "Oriental selection" hot out of circle data
- JS addition, deletion, modification and query of JSON array
- Spark Tuning (II): UDF reduces joins and judgments
- 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
- 不要再说微服务可以解决一切问题了
- Where does this "judge the operation type according to the op value and assemble SQL by yourself" mean? It means simply using Flink tab
- The programmer said, "I'm 36 years old, and I don't want to be rolled, let alone cut."
猜你喜欢

Zero code and high return. How to use 40 sets of templates to meet 95% of the reporting needs in the work

Today's sleep quality record 78 points

浅谈现在的弊端与未来的发展

传统企业要为 Web3 和去中心化做的 11 个准备

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

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

Gold three silver four, don't change jobs

设计一个抢红包系统

亚朵三顾 IPO

基础图表解读“东方甄选”爆火出圈数据
随机推荐
JS import excel & Export Excel
Win11怎么恢复传统右键菜单?Win11右键改回传统模式的方法
The programmer said, "I'm 36 years old, and I don't want to be rolled, let alone cut."
What does security capability mean? What are the protection capabilities of different levels of ISO?
Use mitmproxy to cache 360 degree panoramic web pages offline
Efficient ETL Testing
Please help xampp to do sqlilab is a black
Server SMP, NUMA, MPP system learning notes.
With the help of this treasure artifact, I became the whole stack
After 3 years of testing bytecan software, I was ruthlessly dismissed in February, trying to wake up my brother who was paddling
云原生(三十二) | Kubernetes篇之平台存储系统介绍
Stop saying that microservices can solve all problems
11 preparations for Web3 and Decentralization for traditional enterprises
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
不要再说微服务可以解决一切问题了
Today, I met a senior test developer from Tencent and saw the ceiling of the foundation
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
PDF批量拆分、合并、书签提取、书签写入小工具
Wasserstein Slim GAIN with Gradient Penalty(WSGAIN-GP)介绍及代码实现——基于生成对抗网络的缺失数据填补
Knowledge * review