Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- treeA = [...]
- sumA = ...
- treeB = [...]
- sumB = ...
- // add values from a to b
- For Each a in treeA
- If sumA > sumB AndAlso Abs(sumA - sumB) > Abs(sumA - a, sumB + a) Then
- treeA.Remove(a)
- treeB.Add(a)
- sumA -= a
- sumB += a
- Else
- break
- // add values from b to a
- For Each b in treeB
- If sumB > sumA AndAlso Abs(sumA - sumB) > Abs(sumA + b, sumB - b) Then
- treeB.Remove(b)
- treeA.Add(b)
- sumB -= b
- sumA += b
- Else
- break
- // if we still have time try a 1 for 1 swap
- For Each a in treeA
- For Each b in treeB
- If Abs(sumA - sumB) > Abs(sumA - a + b, sumB - b + a) Then
- treeA.Remove(a)
- treeB.Remove(b)
- sumA = sumA - a + b
- sumB = sumB - b + a
- End If
- Next
- Next
- // depending on time we can try adding single values from a to b and/or b to a again
- // 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