当前位置:网站首页>Complete backpack beginner chapter "suggestions collection"
Complete backpack beginner chapter "suggestions collection"
2022-06-28 13:05:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
More information about backpacks can be found in my “ knapsack ” View in category
One 、 subject
Yes n Class items and a capacity of m The backpack , Each item has an unlimited number of items available . Put in the i The cost of each item is v i , The value is W i . solve : What items to pack in the backpack , The total cost of these items can not exceed that of the backpack Capacity , And the sum of values is the largest .
Two 、 The basic idea
Complete backpack and 01 The difference between backpacks is ,01 There is only one item in the backpack , The complete backpack has an infinite number of items . Let's think in terms of two-dimensional arrays , We are 01 Every time the value is updated in the knapsack, the decision is whether to put it in the first place i Item , And in the complete backpack , What we need to choose is , We are going to put No i Several items ?(0—— The maximum number of pieces can be put )
State transition equation :F[i,j]=max{F[i-1,j],F[i-1,j-k*wi]+k*vi}
The total complexity can be thought of as O(mn ∑ni=0m/vi \sum_{i=0}^n m/vi ) This complexity is very large , We need to improve this complexity .
3、 ... and 、 Simple optimization
If two items i , j Satisfy v i ≤ v j And w i ≥ w j , Then you will be able to j Directly remove , Don't have to consider . This optimization method does not satisfy the worst case , Maybe some topics can use this optimization card to pass the time , Therefore, there is not much description here .
Four 、 Better optimization
Considering the i You can choose the most items m/v i Pieces of , So you can put i Items are converted into m /v i An article of constant cost and value , And then solve this 01 knapsack problem . This does not optimize too much complexity . At this time, we can optimize again through the binary idea , Tied together by several identical items , Add it up and we get each value ( such as , Now our first item ( Just call him a) The best we can do is 5 Pieces of , So let's take a few a Tie it up , The first one a, The second two a, The third four a. We can find that the combination of these parts can be completed 1-5 Pieces of a Into the backpack of the case of traversal ) In this way, each item is divided into O(log ⌊V /C i ⌋) Item , It's a big improvement . Of course, this is still not the best optimization , We still don't use .
5、 ... and 、O(V N ) The algorithm of
Or to 01 Start with the direction of the backpack ,01 Knapsack our final optimization method is to use a one-dimensional array , We traverse from the back to the front , Why? ? We compare the status of the item that has not been loaded yet , So , From the back to the front, data changes can be prevented , At this time, we need to change the data , What we need is that we have selected No i Sub results of items ,( For example, we compare whether to put two first items , What are we comparing ? It is the value of putting one first item and the value of putting two first items ) So at this time, we just need to traverse the second loop from front to back .
CODE:
#include<cstdio>
#include<algorithm>
using namespace std;
int w[300],c[300],f[300010];
int V,n;
int main()
{
scanf("%d%d",&V,&n);
for(int i=1; i<=n; i++)
{
scanf("%d%d",&w[i],&c[i]);
}
for(int i=1; i<=n; i++)
for(int j=w[i]; j<=V; j++)// Notice here , And 0-1 Different backpacks , Here is the order ,0-1 Backpacks are in reverse order
f[j]=max(f[j],f[j-w[i]]+c[i]);
printf("max=%d\n",f[V]);
return 0;
}
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/150680.html Link to the original text :https://javaforall.cn
边栏推荐
- Centos7: switch MySQL users and log in to MySQL
- Stm32f1 and stm32cubeide programming example - matrix keyboard driver
- Online JSON to plaintext tool
- go template with...end遍历用法
- 自定义MySQL连接池
- ##测试bug常用“Redmine”
- I ² C. SMBus, pmbus relationships
- Flink stream processing API collection: master all Flink stream processing technologies. Just read this article
- 4年用户数破亿,孙哥带领波场再创新高
- 如何在熊市中寻找机会?
猜你喜欢
Copying open source for basic software is not advisable. Self reliance is the right way
I ² C. SMBus, pmbus relationships
go template with...end遍历用法
Flink流处理API大合集:掌握所有flink流处理技术,看这一篇就够了
【云原生】自助报表和BI能做这么多事?
Successful cases of rights protection of open source projects: successful rights protection of SPuG open source operation and maintenance platform
The press conference of Tencent cloud Database & CSDN engineer's ability lightweight certification is coming
895. 最长上升子序列
manjaro easyconnecy报错:libgtk-x11-2.0.so.0: cannot open shared object file: No such file or directory
腾讯确认QQ大规模盗号,iPhone14无缘Type-C,第四大运营商5G正式放号,今日更多大新闻在此...
随机推荐
如何在熊市中寻找机会?
Scratch travel photo album Electronic Society graphical programming scratch grade examination level 1 true questions and answers analysis June 2022
Tiantian mathematics serial 53: February 22
嵌入式开发:估算电池寿命的7个技巧
manjaro easyconnecy报错:libgtk-x11-2.0.so.0: cannot open shared object file: No such file or directory
An idea plug-in that automatically generates unit tests, which improves the development efficiency by more than 70%!
The $980000 SaaS project failed
简历模板百度网盘自取
. Net hybrid development solution 24 webview2's superior advantages over cefsharp
SHAREit实力出众,登陆全球 IAP 实力榜 Top7
Centos7 - installing mysql5.7
mysql数据库扫盲,你真的知道什么是数据库嘛
Evaluation of IP location query interface I
5A synchronous rectifier chip 20V to 12v2a/5v4.5a high current 24W high power synchronous rectifier chip high current step-down IC fs2462
Go语学习笔记 - gorm使用 - 数据库配置、表新增 | Web框架Gin(七)
数启扬帆,智聚人才 | 腾讯云数据库 & CSDN 工程师能力轻量认证发布会重磅来袭!...
认识启动函数,找到用户入口
Implementation of fruit and vegetable mall management system based on SSM
Watermaker of the Flink core
flutter 系列之:flutter 中常用的 GridView layout 详解