CHEFMOVR - Editorial

PROBLEM LINK:

PROBLEM EXPLANATION

Given an array consisting of n integers, Chef wants to make all elements of this array equal. He can apply the following operation:

Pick 2 numbers

(i,j) :: j = i + D

Decrementing $A_i$ OR $A_j$ by 1 and adding 1 to the other element. Please help chef by telling the minimum number of operations he needs to make all elements equal, or tell that such situation is impossible.

DIFFICULTY:

PREREQUISITES:

simple

EXPLANATION:

Applying our operation any number of times would keep the sum of elements in our array the same. (Since we are subtracting one from an arbitrary number and adding it to another one), so the sum of our numbers must be divisible by n

The final value of each element would be equal to $\frac{sum}{n}$

In any operation chef would choose 2 numbers (i,j):

(i,j) :: j = i + D

j mod D = i mod D

So you can notice that any pair of elements Ai , Aj such that j mod D ≠ i mod D are independent from each other. (That's true).

That means that we should group our elements by the remainder of their indexes after dividing by D. (And of course) solve each one independently. In fact we will have D groups, the ith group (0 ≤ i < D) contains all elements Aj (j mod D = i)

Let's tell now about handling each set, applying our operation any number of times would keep the sum of elements in our set the same. So the sum of elements in each set must be divisible by this set's size and of course the result of this division must be equal to $\frac{sum}{n}$ , having one set violating this condition would make Chef's mission impossible.

Let's now move to finding the minimum number of operations to fix each set, each set would have 3 kinds of numbers (numbers > $\frac{sum}{n}$) and (numbers < $\frac{sum}{n}$) and of course (numbers equal to $\frac{sum}{n}$) -that we can omit-.

Let's process numbers of each set in the same order of the array (from left to right) and separate them into 2 groups as described (first two types since we can omit the third). Maintaining 2 pointers each one iterating on the elements of one group, would just do the job, because if our processed element is less than $\frac{sum}{n}$ then optimal choice would be picking this number along with the closest number bigger than $\frac{sum}{n}$ to the right of it (the same when the opposite happens), and we should add the distance between them for each increment\decrement operation we apply (because we are using elements between them as mediators). After each iteration, one of the numbers referred to by out pointers would reach the desired value, so we move the pointer on. Practically, this part can be done in simpler way (like author's solution), but this is the detailed explanation. The implementation of this editorial can be found in my code.

AUTHOR'S AND TESTER'S SOLUTIONS:

AUTHOR's solution: Can be found hereTESTER's solution: Can be found hereEDITORIALIST's solution: Can be found here

try out some examples of different size and different D.
you will notice that for some values there will be a range of elements which are not accessible. if all values are same after operations then possibly all element were accessible. see my code for understanding
https://www.codechef.com/viewsolution/14804664

@geforce what i did was ,
1) simply find out the average(avg) of all numbers in the array , and then run a loop from 0-(n-d) in the array. let the loop variable be i then make a[i] the avg value , and if the avg value is less then a[i] then substract it and make a[i]=avg and make a[i+d] + or - the sme number required to make a[i] the avg. at end check if all numbers are avg , if yes then yes else no
you can refer to this https://www.codechef.com/viewsolution/14821344

As in the editorial reduce the problem to $D$ independent subproblems - one for each subset based on residue$\mod D$.

Let $a_1, a_2, ... a_k$ be such a subset. A move operation is performed on two adjacent elements in this subset. Let $\bar a$ be the target (mean) value. Consider partial sums $s_i=\sum_{j=1}^i a_j$. Each $s_i$ for $i < k$ has to be brought to the target value $i \bar a$. $s_k$ must be at the target value, otherwise the mission is unachievable. Observe that a move between $a_i$ and $a_{i+1}$ affects exactly one partial sum - $s_i$, and it changes it by 1. So for each $1 \le i \le k - 1$ we need at least $|s_i - i \bar a| = |\sum_{j=1}^i a_j - i\bar a|=|\sum_{j=1}^i (a_j - \bar a)|$ moves to bring $s_i$ to its target value.

Thus we have the following lower bound for the number of moves: $$\sum_{i=1}^{k-1} \left| \sum_{j=1}^i (a_j - \bar a)\right|$$

It's easy to show that as long as the condition for $s_k$ is satisfied, this bound is always achievable. At each iteration let $a_i, i < k$ be the first element not equal to $\bar a$. If there is no such element - we are done. If $a_i > \bar a$ perform a move operation to $a_{i+1}$. Otherwise, let $j > i$ be the first such that $a_j > 0$ (it must exist). Perform a move operation to $a_{j-1}$. Since at each iteration a partial sum gets closer to its target value by 1, the total number of moves will be equal to the bound.