I am stuck with a problem from Polish olympiad:
every array a1,a2,a3 ... a4
has it's disorder coefficient K, which is equal to |a[1]-a[2]| + |a[2]- a[3]| + |a[3]-a[4]| ... |a[n-1] -a[n]|
. for each element we should calculate minimal K that may be attained by switching places with any other element of the array.
example: Given an array 7 4 5 2 5
.
initial disorder coefficient for this array is
10 = |7-4|+|4-5|+|5-2|+|2-5|
for 1st element minimal disorder coefficient is attained after swapping it with 4th one: |2-4|+|4-5|+|5-7|+|7-5| = 7
. we need to calculate this for all the elements of the array. the complexity should be O(nlogN).
Asked
Active
Viewed 91 times
0

Rezga
- 390
- 1
- 10
-
isn't it the case where if the list becomes something like this - 2 4 5 5 7 after swaping some elements, you get disorder coefficient as 5 .. which is the lowest u can get in order words sorted array will have the minimal disorder coefficient – zenwraight Nov 01 '17 at 18:40
1 Answers
0
Just sort your array and calculate disorder coefficient for this sorted array. It is the MDC of array.
How it works. You need to put together elements which difference is minimum. Sorting will give this result.
Also trying to prove this theory :) will update tomorrow.

Gor
- 2,808
- 6
- 25
- 46