如何将一个集合划分为K个子集,使子集中的元素总和最小?

时间:2015-09-25 21:40:12

标签: c++ algorithm dynamic-programming greedy

我正在研究DP,当我阅读Balanced Partition算法时,我遇到了这个问题。在这个算法中,我们可以将列表分成两个列表,这些列表的元素总和是相等的。但是如果我需要K列表并且我需要它们有一个最小的总和呢?我想过修改Balanced Partition算法来解决这个问题,但实际上我看不到这样做的方法。

设S为{1,1,5},K = 2的最优解为{1,1},{5}。

任何提示? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

一个简单的算法是:

sort S in descending order
for each element in S:
    put it into the partition with the smallest sum

这会将5放入第一个分区,1放入示例中的第二个分区(K = 2)。

相关问题