Is there a simple algorithm to stack N variable-height books evenly into X stacks? I want to place N books of integral heights h1, h2, ... , hN into X stacks as "evenly" as possible.
So if each stack is H1 = ha + hb + ...
and the average stack height A = avg(H1, H2, ... HX)
I want to minimize D = abs(A - H1) + abs(A - H2) + ... + abs(A - HX).
(Forgive the wonky notation, I'm more CS than math.)
For my application, N and X will stay small, so computing power is no object.
I'm also open to alternative measures of "even" stacking. Sum of differences from the mean just seemed like  a decent measure.
 A: Mentally imagine you have a horizontal line drawn on a wall a height $A$ above the floor.
Take the books in any order and stack them into $X$ stacks one stack at a time.
In the first stack, keep stacking books until the height is $\ge A$.
In the second stack, stack the books as high as possible without exceeding a height of $A$.
Continue in this manner until odd numbered stacks have height $\ge A$ and even numbered stacks have height $\le A$.
To avoid having lots of thick or thin books together you might "precondition" the books before stacking by placing the books in increasing order of thickness, then pairing books from opposite ends so that initially they are in order thin, thick, thin, thick, etc.
Now that we have the initial $X$ stacks of books, we can begin the optimization process as follows.
Take an initial measurement of $D$.
For each stack, $k,\,1\le k\le X-1$ and each book $B_{ki}$ in the stack, determine which book in which stack with stack number greater than $k$ will, when swapped with book $B_{ki}$, yield the greatest reduction in $D$. If such a book is found, then swap it with book $B_{ki}$.
When all the stacks have been treated in this way, the process may be repeated anew if desired. Preferably one would have a pre-existing threshold for $\Delta D$ so that when improvements in $D$ were consistently less than $\Delta D$ the process would end.
