简体   繁体   English

给定一部电梯,其最大重量和n个人的重量为x_i,请找出所需的最小乘车次数

[英]Given an elevator with max weight and n people with x_i weights, find out minimum number of rides needed

input:
max_weight = 550
n = 4
x_i = [120, 175, 250, 150]

output:
2  
// [[250, 175, 120], [150]]

My initial impression is that this looks very similar to a dynamic programming coin change/knapsack problem, however it is not coin change (which would be asking for the fewest number of weights to make an exact amount), and it is not knapsack (the weights do not have values and it's like I can have more than 1 knapsack). 我最初的印象是,这看起来与动态编程的找零/背负问题非常相似,但是它不是找零(这将要求最少的砝码数量才能准确确定重量),也不是背负(砝码没有值,就像我可以装多个背包。

Is there a common name/solution for this problem? 这个问题是否有通用名称/解决方案?

This is actually a (1D) Bin Packing problem : 这实际上是(1D)装箱问题

In the bin packing problem, objects of different volumes must be packed into a finite number of bins or containers each of volume V in a way that minimizes the number of bins used . 在垃圾箱包装问题中,必须以最小化使用的垃圾箱数量的方式, 不同体积的物体包装到有限数量的垃圾箱或容器中,每个垃圾箱或容器的体积为V。 In computational complexity theory, it is a combinatorial NP-hard problem. 在计算复杂度理论中,它是一个组合的NP难题。

Here the persons map on the objects en the bins on the rides. 在这里,人员将物体映射到游乐设施上的垃圾箱中。 Like the bin packing problem we want to minimize the number of rides "used" and each person occupies a certain "volume" (that person's weight). 像垃圾箱包装问题一样,我们要尽量减少“使用”的游乐设施的数量,并且每个人都占据一定的“体积”(该人的体重)。

The bin packing problem is - as said in the article - NP-hard. 如文章所述,垃圾箱包装问题是NP-hard。 We can use dynamic programming (but it still has - worst case - exponential time). 我们可以使用动态编程(但它仍然具有-最坏的情况-指数时间)。

The paper A New Algorithm for Optimal Bin Packing by Richard E. Korf discusses an algorithm to solve this problem exactly. 理查德·科夫Richard E. Korf)的《 最佳装箱新算法 》一文讨论了一种精确解决该问题的算法。 It works by using a heuristic approach first and calculating a lower bound, and then use branch and bound to iteratively derive a better solution than the heuristic one until the lower bound is reached, or no solution can be found anymore. 它的工作方式是先使用启发式方法并计算下限,然后使用branch and bound迭代地得出比启发式方法更好的解决方案,直到达到下界,或者找不到解决方案为止。

如果我错了(不是最有效的),请纠正我,但是我认为您可以将所有权重放入max堆中 ,然后使用贪心算法来选择集合。

You're on the right track. 您走在正确的轨道上。 The problem is solved by a modified coin-change algorithm: rather than demanding an exact solution, you find the one that attains the highest total without exceeding the target amount. 通过改进的硬币找零算法可以解决该问题:您无需找到确切的解决方案,而是找到了总额最高而又不超过目标金额的解决方案。 Of course, if you find a solution whose shortfall is less than any remaining set element, you stop and report that solution. 当然,如果找到短缺量小于任何剩余set元素的解决方案,则停止并报告该解决方案。

When you find a solution, remove the "used" weights and iterate until you have allocated them all. 找到解决方案后,请删除“已使用”的权重并进行迭代,直到将所有权重分配完毕为止。 The number of iterations is the quantity of elevators you need. 迭代次数是您需要的电梯数量。

If you sort the elements in descending order, this gives you a "greedy" beginning with backtracking. 如果按降序对元素进行排序,则从回溯开始会给您“贪婪”的感觉。 I suspect that this is reasonably close to optimal for many cases, especially if you memoize the results so that you don't repeat mistakes on the next iteration. 我怀疑这在很多情况下都接近最佳,尤其是当您记住结果以便在下一次迭代中不会重复出现错误时。

You might try some pathological cases, such as a limit of 100, and extreme weights like 您可能会尝试一些病理情况,例如限制为100,以及极端的体重,例如

[93, 91, ..., 5, 4, 4]

The "greedy" algorithm goes for 93+5 first, but later settles for 91+4+4 (closer solution). “贪心”算法首先适用于93 + 5,但后来适用于91 + 4 + 4(更严格的解决方案)。 This is where the memoization comes in handy. 这是方便使用备忘录的地方。

Does that move you toward a solution? 这会推动您走向解决方案吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定 5 个数字,找到中位数所需的最少比较次数是多少? - given 5 numbers, what is the minimum number of comparisons needed to find the median? 给定 N 个人,其中一些是敌人,找出没有敌人的区间数 - Given N people, some of which are enemies, find number of intervals with no enemies 如何以网格方式为两个以上的资产生成约束 \sum{x_i} = 1 的权重? - How to generate weights with constraint \sum{x_i} = 1 for more than two assets in a meshgrid fashion? 找出最大的数,x对于给定的y和n,使得x ^ y &lt;= n - find greatest number, x for given y and n such that x ^ y <= n 使用给定的最小权重最大化子图的数量 - Maximize number of subgraphs with a given minimum weight 给定一个大小的数组NI需要找到将在最小和最大范围内总和的最小值数 - Given an array of size N I need to find the min number of values that will sum up within a min and max range 数字序列 - 找到以 n=0 和 x&gt;=y 结束序列的最小 k - Number Sequence - Find minimum k that ends the sequence with n=0 and x>=y 对于给定的数字N,我如何找到(x和x的因子数)= N的x,ST乘积? - For a given number N, how do I find x, S.T product of (x and no. of factors to x) = N? 保存点 (x_i, y_i) 的数据结构和查找 x_i &gt; a &amp; y_i &gt; b 的所有点的过程 - Data structure that holds points (x_i, y_i) and a procedure to find all points that x_i > a & y_i > b 找到组合,给出n个带有x个球的盒子 - Find the combinations, given n number of box with x number of balls
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM