Editorial for DMOPC '15 Contest 3 P2 - Cheesecake Distribution


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: cheesecake

Knowledge required: array processing, basic math

This was slightly trickier than the usual P2. Notice that we're only concerned with the final state, not the steps we take to get there. Let's denote S as the total number of slices. Right off the bat, we can see that if S is not divisible by N, it is Impossible. If it is divisible, we know that each person has to have \frac S N slices at the end. The observation is that each turn, no matter who's giving to whom, we're moving two steps closer to the end state. The answer is therefore \frac 1 2 \sum_{i=1}^N \left|A[i]-\frac S N\right|.

Time Complexity: \mathcal{O}(N)


Comments

There are no comments at the moment.