寻找最大价值的子集,其中PartitionProblem算法返回true子集、算法、价值、最大

2023-09-11 02:15:11 作者:我向来冰冷成僻却想你好久

从来就得到了下面的任务。

I´ve got the following assignment.

您有一个多集S采用的1< = N< = 22元。 每个元件具有高达千万的正值。

You have a multiset S with of 1<=N<=22 elements. Each element has a positive value of up to 10000000.

Assmuming有两个子组S1和S的s2的其中一个中的所有元素的值的总和等于所述其他的所有元素的值的总和,这是可能的最高值。我必须要返回的的S元素将不被包括在任的两个子集的。

Assmuming that there are two subsets s1 and s2 of S in which the sum of the values of all the elements of one is equal to the sum of the value of all the elements of the other and it is the highest possible value. I have to return which elements of S would not be included in either of the two subsets.

它可能被解决过,我觉得划分问题的一些变种,但我可以'T找到它。如果任何人都可以点我在正确的方向that'd是巨大的。

Its probably been solved before, I think its some variant of the Partition problem but I can´t find it. If anyone could point me in the right direction that´d be great.

编辑:一个不可阻挡的元素在这两个亚群

An element can´t be in both subsets.

推荐答案

划分S尽可能均匀地进入T&杯; U(把额外的元素,如果有的话,U)。遍历分区的T三路进入A&杯; B&杯; C(与乐; 3 11 =其中177147)。存储项|合计(A) - 和(B)| &RARR; C转换映射,仅保留与在情况下,键已存在的最低总和的值。

Partition S as evenly as possible into T ∪ U (put the extra element, if any, in U). Loop through the three-way partitions of T into A ∪ B ∪ C (≤ 311 = 177,147 of them). Store the item |sum(A) - sum(B)| → C into a map, keeping only the value with the lowest sum in case the key already exists.

通过分区的U三通入D&杯环; E&杯; F.查找|之和(D) - 和(E)|在地图上;如果它与C值存在,然后再考虑C&杯; ˚F作为一种可能性的元素离开了(两部分以相等的金额或者是A&杯; D和B&杯; E,或A罩杯,E和B罩杯; D)。

Loop through the three-way partitions of U into D ∪ E ∪ F. Look up |sum(D) - sum(E)| in the map; if it exists with value C, then consider C ∪ F as a possibility for the elements left out (the two parts with equal sum are either A ∪ D and B ∪ E, or A ∪ E and B ∪ D).