题目
The following list of eleven numbers is to be packed into bins of size 55
(a) Use the first-fit bin packing algorithm to pack the numbers into bins of size 55.
(b) Use quick sort to sort the list into descending order, showing the result after each pass. You must clearly identify the pivots used.
(c) Apply the first-fit decreasing bin packing algorithm to pack the numbers into bins of size 55.
题目中文翻译
以下十一个数字列表需要装入容量为 55 的箱子中
(a) 使用首次适应装箱算法将数字装入容量为 55 的箱子。
(b) 使用快速排序将列表按降序排列,显示每轮后的结果。必须清楚标明所使用的基准元素。
(c) 应用首次适应递减装箱算法将数字装入容量为 55 的箱子。
解答
(a)
解法一
思路
展开
保持题目给出的顺序,逐个处理数字。每个数字都从第一个箱子开始检查,放入第一个剩余容量足够的箱子;只有所有已有箱子都放不下时,才启用新箱子。
答题过程
展开
Applying first-fit in the given order gives
| Number | First available bin | Load after placement |
|---|---|---|
| 26 | 1 | 26 |
| 19 | 1 | 45 |
| 31 | 2 | 31 |
| 10 | 1 | 55 |
| 14 | 2 | 45 |
| 30 | 3 | 30 |
| 33 | 4 | 33 |
| 18 | 3 | 48 |
| 3 | 2 | 48 |
| 28 | 5 | 28 |
| 5 | 2 | 53 |
Thus the packing is
| Bin | Numbers | Total |
|---|---|---|
| 1 | 26, 19, 10 | 55 |
| 2 | 31, 14, 3, 5 | 53 |
| 3 | 30, 18 | 48 |
| 4 | 33 | 33 |
| 5 | 28 | 28 |
(b)
解法一
思路
展开
采用官方评分方案中的 middle-right 规则:偶数长度子列选择中间两个元素中的右侧元素作基准。每轮将大于基准的元素放在左侧、小于基准的元素放在右侧,以得到降序排列,并对所有尚未排好的子列同时重复。
答题过程
展开
Using middle-right pivots, begin with pivot 30.
Pass 1:
31 33 |30| 26 19 10 14 18 3 28 5
The next pivots are 33 and 18.
Pass 2:
33 31 |30| 26 19 28 18 10 14 3 5
The next nontrivial pivots are 19 and 3; the one-element partition 31 is already sorted.
Pass 3:
33 31 30 26 28 19 18 10 14 5 3
The next pivots are 28 and 14.
Pass 4:
33 31 30 28 26 19 18 14 10 5 3
The remaining nontrivial pivot is 5; the one-element partition 26 is already sorted.
Pass 5:
33 31 30 28 26 19 18 14 10 5 3
The final one-element pivot is 10. Hence the list in descending order is
解法二
思路
展开
也可采用官方认可的 middle-left 规则:偶数长度子列选择中间左项。只要后续子列持续使用同一规则,也会得到相同的降序结果。
答题过程
展开
Using middle-left pivots, begin with pivot 30.
Pass 1:
31 33 |30| 26 19 10 14 18 3 28 5
The next pivots are 31 and 14.
Pass 2:
33 31 30 26 19 18 28 14 10 3 5
The next nontrivial pivots are 19 and 3; the one-element partition 33 is already sorted.
Pass 3:
33 31 30 26 28 19 18 14 10 5 3
The next nontrivial pivots are 26 and 10; the one-element partition 18 is already sorted.
Pass 4:
33 31 30 28 26 19 18 14 10 5 3
The remaining one-element pivots are 28 and 5. Therefore, the list in descending order is
(c)
解法一
思路
展开
先使用 (b) 的降序列表,再依次应用首次适应规则。每个数字仍要从第一个箱子开始检查,不能为了得到更紧凑的组合而任意调整放置顺序。
答题过程
展开
Using the descending order
first-fit decreasing gives
| Bin | Numbers | Total |
|---|---|---|
| 1 | 33, 19, 3 | 55 |
| 2 | 31, 18, 5 | 54 |
| 3 | 30, 14, 10 | 54 |
| 4 | 28, 26 | 54 |