Minimizing Number of inversions (prove a solution wrong)

Правка en5, от Agassaa, 2016-06-06 17:13:02

I came across a problem in stackoverflow. The problem gives you an array and a value 'K'. You can move an element to right at any position, but you can't move it more than K positions left. Find the optimal ordering on which number of inversions is minimum, e.g. array 3,2,1 has 3inversions. For K=1, solution array is: 2,1,3.

I had a greedy algorithm for this problem, but can't prove its correctness (though I'm pretty sure about its correctness):

iterate the array from left to right.
For each position i we consider a subarray from i to i+k. Then we find the minimum valued element on this subarray and insert this element at the beginning of the subarray and right shift the elements between this interval.
Now, go to position i+1 and do the same.

for example:

given array = 5 3 4 7 8 2 1 0 and K = 2

this algorithm gets the solution array as this:

3 4 5 2 1 0 7 8
minimum inversion value = 12

this is just a naive version of algorithm which we can make faster as O(nlogn).

How would you prove it if this algorithm is right?

Help is greatly appreciated.

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en7 Английский Agassaa 2016-06-06 21:13:18 2 Tiny change: 'K = 2`\n\nthis algori' -> 'K = 2`\n\nThis algori'
en6 Английский Agassaa 2016-06-06 18:00:04 7
en5 Английский Agassaa 2016-06-06 17:13:02 50
en4 Английский Agassaa 2016-06-06 16:10:01 28
en3 Английский Agassaa 2016-06-06 16:06:49 2 Tiny change: '2,1` has `2`inversion' -> '2,1` has `3`inversion'
en2 Английский Agassaa 2016-06-06 16:05:42 8
en1 Английский Agassaa 2016-06-06 16:01:32 1138 Initial revision (published)