[英]N non overlapping Optimal partition
Here is a problem I run into a few days ago. 这是我几天前遇到的一个问题。 Given a list of integer items, we want to partition the items into at most N nonoverlapping, consecutive bins, in a way that minimizes the maximum number of items in any bin.
给定一个整数项列表,我们希望将这些项划分为最多N个不重叠的连续bin,以使任何bin中的最大项数最小的方式。 For example, suppose we are given the items (5, 2, 3, 6, 1, 6), and we want 3 bins.
例如,假设给定项目(5、2、3、6、1、6),我们需要3个箱。 We can optimally partition these as follows:
我们可以按以下方式对它们进行最佳划分:
Every bin has 2 items, so we can't do any better than that. 每个垃圾箱都有2件物品,因此我们不能做得更好。 Can anyone share your idea about this question?
谁能分享您对这个问题的想法?
Given n
bins and an array with p
items, here is one greedy algorithm you could use. 给定
n
垃圾箱和一个包含p
项目的数组,这是您可以使用的一种贪婪算法。
To minimize the max number of items in a bin: 要最大程度地减少垃圾箱中的最大物品数量:
p <= n
Try to use p
bins. p <= n
尝试使用p
槽。
Simply try and put each item in it's own bin. 只需尝试将每个项目放在自己的垃圾箱中即可。 If you have duplicate numbers then your average will be unavoidably worse.
如果您有重复的数字,那么您的平均值将不可避免地变得更糟。
p > n
Greedily use all bins but try to keep each one's member count near floor(p / n)
. p > n
贪婪地使用所有的垃圾箱,但尽量保持每个人的成员数近floor(p / n)
floor(p / n)
with unique numbers to the left and right (if they exist). floor(p / n)
的最大重复箱中进行填充。 Count the number of bins you have and determine the number mergers you need to make, let's call it r
. 计算您拥有的垃圾箱数量,并确定您需要进行合并的数量,我们将其称为
r
。
Repeat the following r
times: 重复以下
r
次:
Example 例
{1,5,6,9,8,8,6,2,5,4,7,5,2,4,5,3,2,8,7,5}
20 items to 4 bins {1,5,6,9,8,8,6,2,5,4,7,5,2,4,5,3,2,8,7,5}
20个项目到4个{1,5,6,9,8,8,6,2,5,4,7,5,2,4,5,3,2,8,7,5}
箱 {1}{2, 2, 2}{3}{4, 4}{5, 5, 5, 5, 5}{6, 6}{7, 7}{8, 8, 8}{9}
1. sorted and grouped {1}{2, 2, 2}{3}{4, 4}{5, 5, 5, 5, 5}{6, 6}{7, 7}{8, 8, 8}{9}
1排序和分组 {1, 2, 2, 2, 3}{4, 4}{5, 5, 5, 5, 5}{6, 6}{7, 7}{8, 8, 8, 9}
2. greedy capture by largest groups {1, 2, 2, 2, 3}{4, 4}{5, 5, 5, 5, 5}{6, 6}{7, 7}{8, 8, 8, 9}
8,8,8,9 {1, 2, 2, 2, 3}{4, 4}{5, 5, 5, 5, 5}{6, 6}{7, 7}{8, 8, 8, 9}
2.贪婪捕获按最大群体 {1, 2, 2, 2, 3}{4, 4}{5, 5, 5, 5, 5}{6, 6}{7, 7}{8, 8, 8, 9}
3. 6 bins but we want 4, so 2 mergers need to be made. {1, 2, 2, 2, 3}{4, 4}{5, 5, 5, 5, 5}{6, 6}{7, 7}{8, 8, 8, 9}
8,8,8,9 {1, 2, 2, 2, 3}{4, 4}{5, 5, 5, 5, 5}{6, 6}{7, 7}{8, 8, 8, 9}
3. 6槽但是我们要4个,因此需要进行2个合并。
{1, 2, 2, 2, 3}{4, 4}{5, 5, 5, 5, 5}{6, 6, 7, 7}{8, 8, 8, 9}
3. first merger {1, 2, 2, 2, 3}{4, 4}{5, 5, 5, 5, 5}{6, 6, 7, 7}{8, 8, 8, 9}
3.第一次合并 {1, 2, 2, 2, 3, 4, 4}{5, 5, 5, 5, 5}{6, 6, 7, 7}{8, 8, 8, 9}
3. second merger {1, 2, 2, 2, 3, 4, 4}{5, 5, 5, 5, 5}{6, 6, 7, 7}{8, 8, 8, 9}
3.第二次合并 So the minimum achievable max was 7. 因此,最小可达到的最大值为7。
Here is some psudocode that will give you just one solution with the minimum bin quantity possible: 这是一些伪代码,将为您提供一种解决方案,并尽可能减少垃圾箱数量:
Sort the list of "Elements" with Element as a pair {Value, Quanity}.
So for example {5,2,3,6,1,6} becomes an ordered set:
Let S = {{1,1},{2,1},{3,1},{5,1},{6,2}}
Let A = the largest quanity of any particular value in the set
Let X = Items in List
Let N = Number of bins
Let MinNum = ceiling ( X / N )
if A > MinNum then Let MinNum = A
Create an array BIN(1 to N+1) of pointers to linked lists of elements.
For I from 1 to N
Remove as many elements from the front of S that are less than MinNum
and Add them to Bin(I)
Next I
Let Bin(I+1)=any remaining in S
LOOP while Bin(I+1) not empty
Let MinNum = MinNum + 1
For I from 1 to N
Remove as many elements from the front of Bin(I+1) so that Bin(I) is less than MinNum
and Add them to Bin(I)
Next I
END LOOP
Your minimum bin size possible will be MinNum
and BIN(1)
to Bin(N)
will contain the distribution of values. 您的最小bin大小可能是
MinNum
, BIN(1)
到Bin(N)
将包含值的分布。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.