简体   繁体   English

N个非重叠最优分区

[英]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: 我们可以按以下方式对它们进行最佳划分:

  • n < 3 : 1, 2 (2 items) n <3 :1,2(2件)
  • 3 <= n < 6 : 3, 5 (2 items) 3 <= n <6 :3,5(2个项目)
  • 6 <= n : 6, 6 (2 items) 6 <= n :6,6(2件)

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)

    1. Group duplicate numbers 组重复号码
    2. Pad the largest duplicate bins that fall short of floor(p / n) with unique numbers to the left and right (if they exist). 用不重复的数字(如果存在)在不符合floor(p / n)的最大重复箱中进行填充。
    3. 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次:

      • Check each possible neighbouring bin pairing; 检查每个可能的相邻垃圾箱配对; find and perform the minimum merger 找到并执行最低限度的合并

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大小可能是MinNumBIN(1)Bin(N)将包含值的分布。

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

相关问题 非重叠购买的最佳顺序 - Optimal sequence of non-overlapping purchases N个重叠会议日程表的最佳房间数和大小 - Optimal room count and sizes for N overlapping Meeting Schedules 非重叠最大评分序列的最优解 - Optimal solution for non-overlapping maximum scoring sequences 在递增的运行时间中对流程进行排序是创建一组非重叠流程的最佳方式吗? - is ordering processes in ascending run time, an optimal way to create a set of non overlapping processes? 给定n个整数集,如何最大化非重叠集的数量 - Given n sets of integers, how to maximize the number of non overlapping sets 在一个时间间隔内随机安排n个非重叠事件 - Random scheduling of n non-overlapping events in a time interval 集{1,2,3,…N}的分区 - Partition of set {1, 2, 3, … N} 如何在给定图中快速计算 n 对非重叠顶点之一不在边上的概率 - How to calculate fast in a given graph the probability that one of n non overlapping pairs of vertices wont be on an edge STL O(log(N)) 算法查找元素是否属于一组非重叠有序区间中的任何区间 - STL O(log(N)) algo to find if an element belongs to any interval in a set of non-overlapping ordered intervals JAVA计算3 ^ n - 3 * 2 ^ n + 3的最佳方法。 - JAVA Optimal way to calculate 3^n - 3*2^n + 3.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM