Editorial for An Animal Contest 5 P4 - Number Game


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.

Authors: WilliamWu277, ThingExplainer

After some observation, we notice that for the bamboo to be stuck, \max(pos-1, N-pos) needs to be less than any s in the set of possible moves remaining. The optimal spot is therefore the centre as \max(pos-1, N-pos) is minimized there, where pos is the location of the bamboo after some number of moves.

So what are the central squares?

If N is even, there are two spots we can move into which are optimal, \frac{N}{2} and \frac{N}{2}+1, which are both central squares; the fact that both these squares are equally optimal will be important later on.

If N is odd, only the square \frac{N+1}{2} is an optimal central square.

It is always optimal to use up the smaller moves before the larger moves, as they provide a larger range of motion. Moreover, we always need to use at least the smallest \left\lfloor \frac{N}{2} \right\rfloor moves, as we can move the bamboo from any position using those moves.

How many steps can we make with the smallest \left\lfloor \frac{N}{2} \right\rfloor moves? We take a total of \frac{\left(\left\lfloor \frac{N}{2} \right\rfloor\right)\left(\left\lfloor \frac{N}{2} \right\rfloor+1\right)}{2}. This is the sum of all the natural numbers up to \left\lfloor \frac{N}{2} \right\rfloor.

Now, we can consider the problem as having two sets and needing to partition 1, 2, \dots, \left\lfloor \frac{N}{2} \right\rfloor into those two sets. One set holds all the moves that go backwards and the other holds all the moves that go forwards. If \frac{\left(\left\lfloor \frac{N}{2} \right\rfloor\right)\left(\left\lfloor \frac{N}{2} \right\rfloor+1\right)}{2} is odd, we can move 1, 3, \dots, \frac{\left(\left\lfloor \frac{N}{2} \right\rfloor\right)\left(\left\lfloor \frac{N}{2} \right\rfloor+1\right)}{2} steps in either direction. If \frac{\left(\left\lfloor \frac{N}{2} \right\rfloor\right)\left(\left\lfloor \frac{N}{2} \right\rfloor+1\right)}{2} is even, we can move 0, 2, \dots, \frac{\left(\left\lfloor \frac{N}{2} \right\rfloor\right)\left(\left\lfloor \frac{N}{2} \right\rfloor+1\right)}{2} steps in either direction, using all \left\lfloor \frac{N}{2} \right\rfloor moves. The proof for this is left as an exercise to the reader.

Thus, for even N, no matter the parity of our starting square X, we can always move into one of the two equally optimal centre positions using the smallest \left\lfloor \frac{N}{2} \right\rfloor = \frac{N}{2} moves; therefore for even N, the answer is always \frac{N}{2}.

For odd N, the answer is only \left\lfloor \frac{N}{2} \right\rfloor if the distance to the central square from the starting square is the same parity as the steps we can take using the first \left\lfloor \frac{N}{2} \right\rfloor moves. In other words, \left\lfloor \frac{N}{2} \right\rfloor is the answer iff:

\displaystyle \left| \frac{N+1}{2}-X \right| \equiv \frac{\left(\left\lfloor \frac{N}{2} \right\rfloor\right)\left(\left\lfloor \frac{N}{2} \right\rfloor+1\right)}{2} \pmod{2}

Otherwise, the answer would be \left\lfloor \frac{N}{2} \right\rfloor+1. Notice that if we use the first \left\lfloor \frac{N}{2} \right\rfloor+1 segments, we can get the object stuck if we move it to positions \frac{N+1}{2}-1, \frac{N+1}{2}, \frac{N+1}{2}+1. Because these three target positions span both parities, we can always make it to one of these three positions.

Now onto the construction. After determining the number of moves we need, we can greedily construct a sequence of moves that works. We will focus on the construction that requires only \left\lfloor \frac{N}{2} \right\rfloor moves, which can be easily extended to include \left\lfloor \frac{N}{2} \right\rfloor+1 moves.

If each move is in the positive direction, our net change in position is \frac{\left(\left\lfloor \frac{N}{2} \right\rfloor\right)\left(\left\lfloor \frac{N}{2} \right\rfloor+1\right)}{2} steps forward. If we only want to move M moves forward, we can go through the configuration of moves in the order \left\lfloor \frac{N}{2} \right\rfloor, \left\lfloor \frac{N}{2} \right\rfloor-1, \dots, 1, keeping track of our net change of position and, if changing the current move s to -s still allows a net final position increase \ge M, we perform the negation and update the final net change of position accordingly. If we instead need to move backwards, we can employ a similar method.

Now that we have a sequence of moves that will land us in the target position, we need to order the moves so that we do not go out of bounds. One way of doing this is creating vectors that store moves forward and moves backwards. We sort both vectors in terms of absolute value and then perform a simulation: we first check if we can make either the biggest move forward or backward, perform a legal move, and update our current position. This solution works in \mathcal{O}(N \log N).

It is left as an exercise to the reader to prove why we do not need to sort the vectors, leading to an \mathcal{O}(N) solution.

Time Complexity: \mathcal{O}(N \log N) or \mathcal{O}(N)


Comments