Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Heuristic, which should work quite good:</p> <pre><code>1. list&lt;int&gt; set1, set2; 2. sort(set2) // decreasing, set2[0] would be the greatest value in set2 3. struct set1item = {set1index, value, list&lt;int&gt; chosen} 4. prepare list&lt;set1item&gt; set1items from set1 //(index = index in set1 list, value = set1[index] and chosen = null) 5. put set1items to some priorityqueue pq // ordered by value 6. for each set2item in set2{ 7. item = pq.first() 8. item.chosen.add(set2item); 9. item.value -= set2item; 10. pq.updateFirst(item) 11.} </code></pre> <p>It would work like: iterate through set2 from highest to lowest, get actual highest element from set1, decrease it by element got from set2 and add this element from set2 to element from set1 results.</p> <p>You must remember to check if all elements from set1 has no empty result.</p> <p>Example1: <code>Set1 = {20, 9, 7, 3}, Set2 = {7, 6, 6, 4, 2, 2, 2, 2, 2, 2, 2, 2}</code>.</p> <p>iter1: <code>fromSet2 = 7</code>, <code>Set1 = {20:{}, 9:{}, 7:{}, 3:{}}</code>, <code>fromSet1=20</code>. Decreasing 20 by 7 and adding 7 to its result. Updated: <code>Set1 = {13:{7}, 9:{}, 7:{}, 3:{}}</code>.</p> <p>iter2: <code>fromSet2 = 6</code>, <code>Set1 = {13:{7}, 9:{}, 7:{}, 3:{}}</code>, <code>fromSet1=13</code>. Decreasing 13 by 6 and adding 6 to its result. Updated: <code>Set1 = {7:{7, 6}, 9:{}, 7:{}, 3:{}}</code>.</p> <p>iter3: <code>fromSet2 = 6</code>, <code>Set1 = {7:{7, 6}, 9:{}, 7:{}, 3:{}}</code>, <code>fromSet1=9</code>. Decreasing 9 by 6 and adding 6 to its result. Updated: <code>Set1 = {7:{7, 6}, 3:{6}, 7:{}, 3:{}}</code>.</p> <p>iter4: <code>fromSet2 = 4</code>, <code>Set1 = {7:{7, 6}, 3:{6}, 7:{}, 3:{}}</code>, <code>fromSet1=7</code>. Decreasing 7 by 4 and adding 4 to its result. Updated: <code>Set1 = {3:{7, 6, 4}, 3:{6}, 7:{}, 3:{}}</code>.</p> <p>iter5: <code>fromSet2 = 2</code>, <code>Set1 = {3:{7, 6, 4}, 3:{6}, 7:{}, 3:{}}</code>, <code>fromSet1=7</code>. Decreasing 7 by 2 and adding 2 to its result. Updated: <code>Set1 = {3:{7, 6, 4}, 3:{6}, 5:{2}, 3:{}}</code>.</p> <p>...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload