Advertisement
Guest User

Untitled

a guest
Sep 4th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. treeA = [...]
  2. sumA = ...
  3. treeB = [...]
  4. sumB = ...
  5.  
  6. // add values from a to b
  7. For Each a in treeA
  8. If sumA > sumB AndAlso Abs(sumA - sumB) > Abs(sumA - a, sumB + a) Then
  9. treeA.Remove(a)
  10. treeB.Add(a)
  11. sumA -= a
  12. sumB += a
  13. Else
  14. break
  15.  
  16. // add values from b to a
  17. For Each b in treeB
  18. If sumB > sumA AndAlso Abs(sumA - sumB) > Abs(sumA + b, sumB - b) Then
  19. treeB.Remove(b)
  20. treeA.Add(b)
  21. sumB -= b
  22. sumA += b
  23. Else
  24. break
  25.  
  26. // if we still have time try a 1 for 1 swap
  27. For Each a in treeA
  28. For Each b in treeB
  29. If Abs(sumA - sumB) > Abs(sumA - a + b, sumB - b + a) Then
  30. treeA.Remove(a)
  31. treeB.Remove(b)
  32. sumA = sumA - a + b
  33. sumB = sumB - b + a
  34. End If
  35. Next
  36. Next
  37.  
  38. // depending on time we can try adding single values from a to b and/or b to a again
  39. // another option is to try a swap 2 from a with 1 from b and vice versa, once you start with combinations Pick 2+ your running time will increase substantially
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement