partition a sequence of 2n real numbers so that

后端 未结 3 2049
深忆病人
深忆病人 2021-02-15 06:07

I\'m currently reading The Algorithm Design Manual and I\'m stuck on this exercise.


Take a sequence of 2n real numbers as input. Design an O(n log n) algorithm tha

相关标签:
3条回答
  • 2021-02-15 06:27

    Given the input array

    x0 x1 x2 ... x2n

    use merge sort to sort it in O( n log n) time to produce a sorted list

    xa0 xa1 ... xa2n

    where the indices ai indicate the permutation that you have performed on the initial list to obtain the second list.

    I claim then that the pairing that produces the minimum maximum sum over all pairings is the following pairing:

    (xai, xa2n-i) for i = 0, 1, ..., n. That is, you group the highest valued number with the lowest valued number available.

    Proof

    Proceed by induction, for the case 2n=2 this is obvious.

    Without loss of generality consider that the input is a list of sorted numbers (since if it is not then sort them)

    x0 x1 x2 ... x2n.

    Consider the pairing of x2n with any number, then clearly the minmum sum of this pairing is achieved with (x2n, x0).

    Now consider the pairing of x2n-1, either (x2n,x0),(x2n-1,x1) is the pairings that produce the minumim max sum, or (x2n,x1),(x2n-1,x0) is, or both are. In the latter case our choice doesn't matter. In the seconds to last case, this is impossible (think about it.) In the general case if we proceed inductively by this process, when we are looking for a pair for x2n-k, xk is the lowest unused value that we can pair up with, however; suppose instead we pair up xk with some other x2n-j for j < k, to try to get a lower minum sum. This is impossible as, x2n-j + xk >= x2n-k + xk, so at most we can only achieve the same minimum sum.

    This means that choosing (x2n-k,xk) gives us the minimum pairings.

    0 讨论(0)
  • 2021-02-15 06:32

    I think I can prove this for a sequence with no duplicated numbers, and it should be a reasonably simple exercise for the reader to extend the proof to non-unique sequences.

    Pair x0, x2n together, then pair all other numbers according to an optimal solution.

    Now consider the pairing of (x0, x2n) against any other pair xy, xz from the optimal subset. x2n + either xy or xz will be greater than xy+xz and also x2n+x0, therefore the pairing of x2n, x0 was optimal.

    The proof now extends by induction to the pairing of X1, X2n-1, and further partitions of the subset, eventually producing the OP's pairing.

    0 讨论(0)
  • 2021-02-15 06:46

    The algorithm works because when x0, x1, ... x2n-1 is the sorted list, there is always an optimal solution that contains (x0, x2n-1).

    Proof:

    Consider any optimal solution which does not contain (x0, x2n-1). It must contain pairs (x0, xa) and (xb, x2n-1) with x0 ≤ xa ≤ x2n-1 and x0 ≤ xb ≤ x2n-1. Remove those pairs from the solution, and in their place put (x0, x2n-1) and (xa, xb). Could the presence of either new pair have "damaged" the solution? The pair (x0, x2n-1) could not have, since its sum is less than or equal to the sum of (xb, x2n-1) which was a member of the original, optimal solution. Neither again could (xa, xb) have caused damage, since its sum is less than or equal to the sum of (xb, x2n-1), which was a member of the same solution. We have constructed an optimal solution which does contain (x0, x2n-1).

    Thus the algorithm you give never forecloses the possibility of finding an optimal solution at any step, and when there are only two values left to pair they must be paired together.

    0 讨论(0)
提交回复
热议问题