UVa 410 - Station Balance

Summary
Mathematically, given a list of 2n integers, we need to match them in pairs such that the imbalance factor defined in the problem is minimum.

Explanation and Proof
Since each chamber can only hold at most $$2$$ specimens, the greedy algorithm works here. Let $$n$$ be the number of chambers. To simplify, we always assume that there are $$2n$$ specimens. If there are not so many, we can zero-pad it by adding some specimens of mass $$0$$. Let the (ascending) sorted list of specimen masses be {$$A_1, A_2, ..., A_{2n}$$}, we match $$A_1$$ with $$A_{2n}$$, $$A_2$$ with $$A_{2n-1}$$, ..., $$A_i$$ with $$A_{2n-i}$$, ..., and put them into separate chambers. This way, the imbalance is guaranteed to be the smallest.

Why does the greedy algorithm works for this problem? Followed is a proof. We exploit the fact that each chamber can only hold at most $$2$$ specimens.

We have $$A_1 \leq A_2 ... \leq A_{2n}$$. In the $$k$$th step, we try to match $$A_k$$. Let us just look into the first step, since the rest can be done by induction. The greedy algorithm above matches $$A_1$$ with $$A_{2n}$$. Let us denote it by { $$(A_1,A_{2n})$$ }. We want to show that the match { $$(A_1,A_{2n})$$ } is at least as good as any other match { $$(A_1,A_j)$$ } where $$1<j<2n$$.

Suppose in the match { $$(A_1,A_j)$$ }, we have also matched $$A_{2n}$$ to $$A_i$$ where $$1<i<2n$$ and $$i \not= j$$. Let us denote this alternative by { $$(A_1,A_j)$$, $$(A_i,A_{2n})$$ }. We claim that the match { $$(A_1,A_{2n})$$, $$(A_i,A_j)$$ } is at least as good this alternative. Since we get the greedy match by simply switching $$A_j$$ and $$A_{2n}$$, the imbalance contributions from the other chambers are not changed. Let $$M$$ be the average chamber mass. We want to show that

$$| A_1 + A_{2n} - M | + | A_i + A_j - M | \leq | A_1 + A_j - M | + | A_i + A_{2n} - M |$$.

For ease of notation, we write $$a = A_1, b = A_i, c = A_j, d = A_n$$, which is

$$| a + d - M | + | b + c - M | \leq | a + c - M | + | b + d - M | \cdots (\ast)$$

Observations:
 * $$a \leq b, c \leq d$$
 * $$a + c \leq a + d$$
 * $$b + c \leq b + d $$

To prove $$(\ast)$$, we need to prove it for each possible combination of the $$4$$ absolute value symbols (the insides of each can be either $$\geq 0$$ or $$\leq 0$$). (If $$x \leq 0$$, then $$|x| = - x$$) Fortunately, due to the observations, there are only 2 possible orderings


 * $$a + c \leq a + d \leq b + c \leq b + d$$ or
 * $$a + c \leq b + c \leq a + d \leq b + d$$.

So there are six cases:
 * If $$M \leq a + c$$ then the insides of the $$|...|$$ in $$(\ast)$$ are all positive, equivalent to $$0\leq 0$$ which is true.
 * If $$b + d \leq M$$ then the insides of the $$|...|$$ in $$(\ast)$$ are all negative, equivalent to $$0\leq 0$$ which is true.
 * If $$a + c \leq M \leq a + d, b + c$$ then the insides of the $$|...|$$ in $$(\ast)$$ are $$+, +, -, +$$, equivalent to $$a + c \leq M$$ which is assumed.
 * If $$a + d, b + c \leq M \leq b + d$$ then the insides of the $$|...|$$ in $$(\ast)$$ are $$-, -, -, +$$, equivalent to $$M \leq b + d$$ which is assumed.
 * If $$a + d \leq M \leq b + c$$ then the insides of the $$|...|$$ in $$(\ast)$$ are $$-, +, -, +$$, equivalent to $$c \leq d$$ which is true.
 * If $$b + c \leq M \leq a + d$$ then the insides of the $$|...|$$ in $$(\ast)$$ are $$+, -, -, +$$, equivalent to $$a \leq b$$ which is true.

So, we know that { $$(A_1,A_{2n})$$ } is an optimal matching. By induction, considering the shorter (length $$2(n-1)$$) list, we are done. The base case with $$2$$ elements is obvious.

Gotchas

 * As long as the matching is correct, it does not matter which chamber you put a pair into.
 * Do not forget the space before the chamber ID.

Input
2 3 6 3 8 3 5 51 19 27 14 33 5 9 1 2 3 5 7 11 13 17 19

Output
Set #1 0: 6 3 1: 8 IMBALANCE = 1.00000

Set #2 0: 51 1: 19 27 2: 14 33 IMBALANCE = 6.00000

Set #3 0: 1 17 1: 2 13 2: 3 11 3: 5 7 4: 19 IMBALANCE = 11.60000