Proving a set of requirements can be met with a set of values using LINQ

孤者浪人 提交于 2019-12-06 01:01:59

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.

Edit: 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:

  • 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
  • At the end of all this you will have the vials used to fill bucket b. Then you just remove those from the set of available vials, and move on to the next bucket.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!