# SEASHUF - Editorial

#PROBLEM LINK:
[Practice][111]
[Contest][222]
**Author:** [Sergey Nagin][4444]
**Tester:** [Praveen Dhinwa][5555] and [Hiroto Sekido][123]
**Editorialist:** [Lalit Kundu][7777]
#DIFFICULTY:
Challenge
#PREREQUISITES:
Greedy, Heuristics
#PROBLEM:
<p>Sereja have an array <b>A = [A<sub>1</sub>, A<sub>2</sub>, ..., A<sub>N</sub>]</b>, where <b>N</b> is an even integer. Let the function <b>f(A)</b> be defined by <b>f(A) = |(A<sub>1</sub> + A<sub>2</sub> + ... + A<sub>N/2</sub>) − (A<sub>N/2+1</sub> + A<sub>N/2+2</sub> + ... + A<sub>N</sub>)|</b></b>. Sereja want to decrease the value <b>f(A)</b> by the unusual way.</p>
<p>Let <b>A[l..r]</b> denote the sub-array <b>[A<sub>l</sub>, A<sub>l+1</sub>, ..., A<sub>r</sub>]</b> of <b>A</b>. Sereja can shift some sub-array <b>A[l..r]</b>, where <b>1 ≤ l ≤ r ≤ N</b>, then the array <b>A</b> will become <b>[A<sub>1</sub>, A<sub>2</sub>, ..., A<sub>l-1</sub>, A<sub>l+1</sub>, A<sub>l+2</sub>, ..., A<sub>r</sub>, A<sub>l</sub>, A<sub>r+1</sub>, A<sub>r+2</sub>, ..., A<sub>N</sub>]</b>. For example we have the array <b>A = [1, 2, 3, 4, 5]</b> and we choose <b> l = 2, r = 4</b>, then after shifting we have the following array: <b>A = [1, 3, 4, 2, 5]</b>.</p>
<p>Sereja can shift multiple times, however it's burdensome to shift many elements. Thus the sum of <b>r − l + 1</b> should not exceed <b>2 × N</b>.</p>
<p>You have to minimise the final value of F(A).</p>
#EXPLANATION:
There are two halves in the array say lowerhalf(1 to N/2) and upperhalf(N/2+1 to N). Suppose upperhalf has more weight(sum of elements) than lowerhalf. We'll try to swap elements between upperhalf and lowerhalf such that difference of their weight is as small as possible.
Note that we can swap elements A<sub>i</sub> and A<sub>j</sub> by repeatedly perfoming the operation [L,R] on the array.
For example, in array 1,2,3,4,5,6 we need to swap element 2 and element 5: applying operations [2,5], [2,4], [2,4].
So, swapping two elements has a cost equal to sum of (R-L+1) of each operation we apply. We can have total of all swaps not more than 2\*N.
So swapping costs us some points out of 2\*N, but it also decreases F(A). So, we can greedily make swaps or we can make heuristics based on the cost and the profit we get.
One approach could be:
Sort lowerHalf of array using a heuristics which gives elements nearer to center(N/2) and elements smaller in magnitude more priority.
Sort UpperHalf of array using a heuristic which gives elements nearer to center and elements higher in magnitude more priority.
Now For each element in lowerHalf (starting from center to left end) find the element in righthalf which has more value and swap them.
This is a challenge problem and people always come with great ideas :).
I welcome everybody to explain their approach on this thread.
#AUTHOR'S AND TESTER'S SOLUTIONS:
[Author's solution][131]
[Tester's solution][132]
[111]: http://www.codechef.com/problems/SEASHUF
[222]: http://www.codechef.com/AUG14/problems/SEASHUF
[131]: http://www.codechef.com/download/Solutions/2014/August/Setter/SEASHUF.cpp
[132]: ~~http://www.codechef.com/download/Solutions/2014/August/Tester/REVERSE.cpp
~~http://www.codechef.com/download/Solutions/2014/August/Tester/SEASHUF.cpp
[4444]: http://www.codechef.com/users/sereja
[5555]: http://www.codechef.com/users/dpraveen
[7777]: http://www.codechef.com/users/darkshadows
[123]: http://www.codechef.com/users/laycurse