Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <a href="https://stackoverflow.com/questions/4239215/getting-the-lowest-possible-sum-from-numbers-difference/4239849#4239849">solution by marcog</a> is a correct, non-recursive, polynomial-time solution to the problem — it's a pretty standard DP problem — but, just for completeness, here's a proof that it works, and actual code for the problem. [@marcog: Feel free to copy any part of this answer into your own if you wish; I'll then delete this.]</p> <h3>Proof</h3> <p>Let the list be x<sub>1</sub>, …, x<sub>N</sub>. Assume wlog that the list is sorted. We're trying to find K (disjoint) pairs of elements from the list, such that the sum of their differences is minimised.</p> <p><strong>Claim</strong>: An optimal solution always consists of the differences of consecutive elements.<br> <strong>Proof</strong>: Suppose you fix the subset of elements whose differences are taken. Then by the <a href="https://stackoverflow.com/questions/4239215/getting-the-lowest-possible-sum-from-numbers-difference/4239390#4239390">proof given by Jonas Kölker</a>, the optimal solution <em>for just this subset</em> consists of differences of consecutive elements from the list. Now suppose there is a solution corresponding to a subset that does not comprise pairs of consecutive elements, i.e. the solution involves a difference x<sub>j</sub>-x<sub>i</sub> where j>i+1. Then, we can replace x<sub>j</sub> with x<sub>i+1</sub> to get a smaller difference, since<br> x<sub>i</sub> ≤ x<sub>i+1</sub> ≤ x<sub>j</sub> ⇒ x<sub>i+1</sub>-x<sub>i</sub> ≤ x<sub>j</sub>-x<sub>i</sub>.<br> (Needless to say, if x<sub>i+1</sub>=x<sub>j</sub>, then taking x<sub>i+1</sub> is indistinguishable from taking x<sub>j</sub>.) This proves the claim.</p> <p>The rest is just routine dynamic programming stuff: the optimal solution using k pairs from the first n elements either doesn't use the nth element at all (in which case it's just the optimal solution using k pairs from the first n-1), <strong><em>or</em></strong> it uses the nth element in which case it's the difference x<sub>n</sub>-x<sub>n-1</sub> plus the optimal solution using k-1 pairs from the first n-2.</p> <p>The whole program runs in time O(N log N + NK), as marcog says. (Sorting + DP.)</p> <h3>Code</h3> <p>Here's a complete program. I was lazy with initializing arrays and wrote Python code using dicts; this is a small log(N) factor over using actual arrays.</p> <pre><code>''' The minimum possible sum|x_i - x_j| using K pairs (2K numbers) from N numbers ''' import sys def ints(): return [int(s) for s in sys.stdin.readline().split()] N, K = ints() num = sorted(ints()) best = {} #best[(k,n)] = minimum sum using k pairs out of 0 to n def b(k,n): if best.has_key((k,n)): return best[(k,n)] if k==0: return 0 return float('inf') for n in range(1,N): for k in range(1,K+1): best[(k,n)] = min([b(k,n-1), #Not using num[n] b(k-1,n-2) + num[n]-num[n-1]]) #Using num[n] print best[(K,N-1)] </code></pre> <p>Test it:</p> <pre><code>Input 4 2 1515 1520 1500 1535 Output 30 Input 8 3 1731 1572 2041 1561 1682 1572 1609 1731 Output 48 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload