问题描述
这是张贴问题<一个子集href="http://stackoverflow.com/questions/16703082/crafting-a-linq-based-solution-to-determine-if-a-set-of-$p$pdicates-are-satisfied/16703389#16703389">here.
给定一组体积的水桶 B = {X1,X2,...,XN}
和一组瓶,容积 V = {V1,V2,...,VN} 什么是证明桶的数目可以填充有假定小瓶必须所有灌入小瓶的内容的最佳方式一斗。溢出是允许的。
Given a set of buckets of volume B={x1, x2, ..., xn}
and a set of vials with liquid of volumes V={v1, v2, ..., vn }
what is the best way to prove that the number of buckets can be filled with the contents of the vials assuming that vials must be poured all into one bucket. Overflow is permitted.
下面一些明显的不变量是桶的基数| B |
必须小于或等于小瓶的基数| V |
,而且桶的总容积和(B)
必须小于或等于瓶的总体积总和(V)
Some obvious invariants here are that the cardinality of the buckets |B|
must be less than or equal to the cardinality of the vials |V|
and that the combined volume of the buckets Sum(B)
must be less than or equal to the combined volume of the vials Sum(V)
这是一个众所周知的计算问题?如果是的话可以简单的LINQ的解决方案来精雕细琢的前preSS这在C#?
Is this a well known computational problem? If so can a simple LINQ solution be crafted to express this in C#?
我觉得这是一件埃里克利珀会在博客; - 。)
I feel like this is something Eric Lippert would have blogged about ;-).
推荐答案
考虑这个问题,你有同样大小的两个桶的实例,与和(B)= SUM(V)。这意味着你需要同样在这两个桶分发瓶,否则人会溢出,也不会有足够的留给其他。这就是所谓的划分问题和它已知是NP完全问题。
Consider an instance of this problem where you have two buckets of the same size, and Sum(B) = Sum(V). This means you need to distribute the vials equally over the two buckets, otherwise one will overflow and there won't be enough left for the other. This is called the partition problem, and it is known to be NP-complete.
编辑:当然,NP完全并不意味着问题不能得到解决,这一点的运行时间将是指数在输入的大小(在此情况下,最大的桶大小的LOG2)。
Of course, NP-completeness doesn't mean the problem can't be solved, just that the running time will be exponential in the size of the input (in this case, the log2 of the biggest bucket size).
如果我们能找到的液体需要填补一个桶(包括溢出)的最小量,解决这个问题是每个桶后,这样对每个桶,除去所用的小瓶从一组可用的小瓶的一个简单的事情
If we can find the smallest amount of liquid needed to fill a bucket (including spillage), solving the problem is a simple matter of doing this for each bucket, and removing the used vials from the set of available vials after each bucket.
我们可以通过使用动态规划做到这一点:
We can do this by using dynamic programming:
- 对于给定的铲斗B,考虑大小为0的所有桶补足体积(b)中。
- 尺寸0桶显然不需要液体
- 对于每一个大小S,找到一个小瓶v的:
- 对于s体积(v)不使用v 将溶液
- (的用于液体量S-体积(v))+卷(v)的最小化
- For a given bucket b, consider all the buckets of size 0 up to volume(b).
- The size 0 bucket obviously requires no liquid
- For each size s, find a vial v such that:
- The solution for s-volume(v) does not use v
- (The amount of liquid used for s-volume(v)) + volume(v) is minimized
这篇关于证明了一组要求,可满足一组使用LINQ值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!