PROBLEM LINK:Author: Dmytro Berezin PROBLEM EXPLANATIONGiven 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 A_{i} , A_{j} 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 i_{th} group (0 ≤ i < D) contains all elements A_{j} (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 here
This question is marked "community wiki".
asked 11 Aug '17, 14:17

I solved this problem easier. First I determined the target value (as explained in editorial). Go through the array right to left. At each index, we take as many units from the left corresponding array index (if there is one) as is necessary to bring the value to the target. If the resulting array is equal, we output it. Otherwise, no solution exists. Example:
Resulting array is equal, so we output it. answered 18 Aug '17, 16:30

The problem PRLADDU set by me is a subproblem of this problem. Its editorial is really nicely written and explains a nice way of proving the greedy approach of this solution. answered 18 Aug '17, 16:49

is there anybody who has an easy solution for this question . I can only understand this solution upto this line In any operation chef would choose 2 numbers (i,j): After that I cannot understand what is the solution trying to tell . somewhere its saying j mod d = I mod d then saying j mod d is not equal to d ??? I cannot understand this after the line above. please help . thanks in advance answered 19 Aug '17, 19:53
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
(19 Aug '17, 20:15)

Answer is hidden as author is suspended. Click here to view.
answered 19 Aug '17, 19:59

Can anyone please explain to me why there's this condition in Author's solution? Thanks
answered 21 Aug '17, 09:26

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}^{k1} \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_{j1}$. 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. Here is the code. answered 23 Aug '17, 01:50

I am doing the same thing as AUTHOR's solution in C, but still I am getting WA in Task 3 of SubTask 2. include<stdio.h>include<stdlib.h>include<math.h>int main(void) { long long int t,n,d,i,sum,di,a[100015],f,c,j,s,ans; scanf("%lld",&t); while(t) { scanf("%lld%lld",&n,&d); sum=0; for(i=0;i<n;i++) { scanf("%lld",&a[i]); sum+=a[i]; } ans=0; if(sum%n==0) { di=sum/n; for(i=0;i<d;i++) { s=f=c=0; for(j=i;j<n;j+=d) { s+=a[j]di; ans+=abs(s); c+=a[j]; f++; } if(c%f!=0) { ans=1; break; } } } else ans=1; printf("%lld\n",ans); } return 0; } Can you please see where am I getting it wrong? answered 25 Aug '17, 22:39

The link to solution wrong. answered 01 Sep '17, 08:39
