Sketch of a heuristic algorithm
I have no exact solution for this problem. But since Raphael's
comment suggests it looks like the partition problem, for which
heuristic algorithms have been developed, I will try a heuristic
approach. This is only a sketch of a heuristic algorithm.
Let $v$ be the number of values, and let $n$ be the number of elements
in the list. We assume without loss of generality that the values are
the numbers in $[1..n]$. For each value $i$ we note $n_i$ the number
of its occurrences in the list.
We first note that the sum of the distances for all occurrences of a
value is $n$. So the sum of all distances for all elements of the list
is $vn$. Hence, the average distance for all elements of the list is
$vn/n$, i.e., $v$.
Our purpose is to minimize the standard deviation on distances, which
amounts to minimizing the sum of the squares of distance deviations,
i.e. of differences between $v$ and each distance.
When placing the occurrences of one value, we try to equalize
distances, up to integer constraints (and to the fact that slots may
be occupied at some point). That is how they contribute the least to
the standard deviation. If we are forced to increase a distance, it is
always better to increase one that deviates the least. The the optimal
distance for a value $i$ is $n/n_i$. Taking into account integer
constraints, $n\mod n_i$ occurrences should use the integer value just
above $n/n_i$, and the others just below.
That will guide our algorithm.
But first, we note that singleton values (occurring only once) will always
have the same associated distance $n$. Hence their placement does not
matter and can be ignored by the algorithm. They will just take
whatever slots are left available at the end.
Then, since those distances that deviate most have to be the most
exact to contribute less to the sum of squares, we try to place first
the values that necessarily deviate most, i.e. the values $i$ such that
$|n/n_i -v|$ is greatest.
It may be a value with very many of very few occurrences at first. I
think it does not actually make a difference, since the constraints
created by occupying slots are in proportion of the number of values
well (?) placed.
Thr first value considered can be placed without any constraint. Then
The other values must be placed so as to minimize their contribution
to the standard deviation, but only in the slots left free by whatever
values have been placed before.
The placement of the occurrences of a value in the remaining slots can
be done with a dynamic programming algorithm, so as to merge
computations that place the same number of values between two
positions, keeping only those that have minimal contribution to the
standard deviation (i.e. minimum value for the sum of the square of
their deviations).
Occasionally, there will be several minimal solutions. In that case
you try to preserve some slack by choosing the minimal solution that
has the remaing slots most evenly distributed. THis can be computed,
for each solution, by computing the standard deviation of the
distances between the remaining free slots (with repect to their mean value, not with respect to $v$).
Then you repeat for the next remaining value $j$ such that $|n/n_j
-v|$ is greatest, an so on until all non singleton values are placed.
Then you put the singleton values in the remaining slots.
I believe this should generally give reasonable solution, but I have
yet no idea on how to prove it or estimate the gap with an optimal
solution.