CSU-ACM2019寒假集训 DP初步-E - Bone Collector

本文介绍了01背包问题,通过一个实例展示了如何计算骨头收藏家能获得的最大价值。题目中,收藏家有一个固定体积的袋子,需要决定哪些骨头应该被收入,以最大化总价值。问题的关键在于理解动态规划的函数意义,并正确处理不同容量限制下的物品选择。最后给出了问题的解决方案和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这道题目就开始01背包问题了,emmmm,刚开始确实很难懂o(╥﹏╥)o

题目:

Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?
在这里插入图片描述
Input
The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.

Output
One integer per line representing the maximum of the total value (this number will be less than 2^31).

Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1

Sample Output
14

日常翻译:
许多年前,在泰迪的家乡,有一个人被称为“骨头收藏家”。这个男人喜欢收集各种各样的骨头,比如狗的,牛的,还有他去了坟墓…
骨收集器有一个体积为V的大袋子,沿着他的收集之旅有很多骨头,显然,不同的骨骼有不同的价值和不同的体积,现在根据他的行程给出每个骨头的值,你能计算吗?超出骨收集器可以获得的总价值的最大值?

输入
第一行包含整数T,个案数。
接下来是T个案例,每个案例有三行,第一行包含两个整数N,V,(N <= 1000,V <= 1000)表示骨骼的数量和他的包的体积。第二行包含表示每个骨骼值的N个整数。第三行包含表示每个骨骼体积的N个整数。

产量
每行一个整数表示总值的最大值(该数字将小于2 ^ 31)。

样本输入
1
5 10
1 2 3 4 5
5 4 3 2 1

样本输出
14

思路:这道题是个01背包的问题,首先给出关系好吧(╯▽╰),然后这里存在一个困扰:如果有多余空间去放置的话,那么放置肯定比不放的价值大啊,因为我多放入一个物品了啊,也就是说选择不放的函数f(n−1,x),f(n−1,x)肯定小与放置的函数f(n−1,c−w)+v,说道这里你就犯了一个惯性错误,认为f(n−1,x)与f(n−1,c−w)所对应的F(n,c,x)中的x={x1,x2,…xn}组合相同,在理解这个函数的意义:从n个物品里面选物品,容量为C,能达到的最大价值。这个两个函数组合不一定相同,为容量C约束条件变了。就会变为从n-1个物品里面选,容量为C与从n-1个物品里面选,容量为c-w且加上v,两者哪个大。彻底理解了函数f之后,我相信没有什么能够阻挡你学习这个算法了

f[i][j]表示前i个物品,背包大小为j时,可获得的最大价值。
对于物品i  
f[i][j]=max(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值