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: ThingExplainer
Subtask 1
Note that we only care about the parity of each alpaca's happiness index. For the
alpaca, we can set
for an even happiness index and
for an odd happiness index.
We can then brute force all possible numberings and check if any are valid.
Time Complexity: 
Subtask 2
First, let's consider the case where there is no possible solution.
If
is not even, we can prove that there is no possible numbering. This is because if we start with all alpacas happy and switch the parity of a single alpaca, it causes
alpacas to become sad. We can observe that it is impossible to increment or decrement the number of happy alpacas by
when changing the parity of any alpaca; therefore, the only values of
that yield a possible numbering when given
are
,
,
, and so on.
If the input is not of the above mentioned case, the given case has a valid solution.
Using the observation in Subtask 1, we can simply initialize our array with zeroes (all even). Note that at this stage, all alpacas are happy. We observe that if we flip an alpaca from even to odd, we decrease the number of happy alpacas by
. Thus, the number of parity flips we have to make to this array is
. Note that adjacent flips have no effect and should be avoided for simplicity.
We can now iterate through the array and make
parity flips, making sure not to make a flip adjacent to a previous flip.
Time Complexity: 
Comments