java - custom partition problem -


could 1 guide me on how solve problem.

we given set s k number of elements in it.

now have divide set s x subsets such difference in number of elements in each subset not more 1 , sum of each subset should close each other possible.

example 1: {10, 20, 90, 200, 100} has divided 2 subsets

solution:{10,200}{20,90,100}

sum 210 , 210

example 2: {1, 1, 2, 1, 1, 1, 1, 1, 1, 6}

solution:{1,1,1,1,6}{1,2,1,1,1}

sum 10 , 6.

i see possible solution in 2 stages.

stage one

start selecting number of subsets, n. sort original set, s, if possible. distribute largest n numbers s subsets 1 n in order. distribute next n largest numbers s subsets in reverse order, n 1. repeat until numbers distributed.

if can't sort s, distribute each number s subset (or 1 of subsets) least entries , smallest total.

you should have n subsets sized within 1 of each other , similar totals.

stage two

now try refine approximate solution have. pick largest total subset, l, , smallest total subset, m. pick number in l smaller number in m not increase absolute difference between 2 subsets. swap 2 numbers. repeat. not pairs of subsets have swappable numbers. each swap keeps subsets same size.

if have lot of time can thorough search; if not try pick off few obvious cases. don't swap numbers if there no decrease in difference; otherwise might infinite loop.

you interleave stages once there @ least 2 members in subsets.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -