Editorial for An Animal Contest 5 P4 - Number Game
Submitting an official solution before solving the problem yourself is a bannable offence.
After some observation, we notice that for the bamboo to be stuck, needs to be less than any in the set of possible moves remaining. The optimal spot is therefore the centre as is minimized there, where is the location of the bamboo after some number of moves.
So what are the central squares?
If is even, there are two spots we can move into which are optimal, and , which are both central squares; the fact that both these squares are equally optimal will be important later on.
If is odd, only the square 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 moves, as we can move the bamboo from any position using those moves.
How many steps can we make with the smallest moves? We take a total of . This is the sum of all the natural numbers up to .
Now, we can consider the problem as having two sets and needing to partition into those two sets. One set holds all the moves that go backwards and the other holds all the moves that go forwards. If is odd, we can move steps in either direction. If is even, we can move steps in either direction, using all moves. The proof for this is left as an exercise to the reader.
Thus, for even , no matter the parity of our starting square , we can always move into one of the two equally optimal centre positions using the smallest moves; therefore for even , the answer is always .
For odd , the answer is only if the distance to the central square from the starting square is the same parity as the steps we can take using the first moves. In other words, is the answer iff:
Otherwise, the answer would be . Notice that if we use the first segments, we can get the object stuck if we move it to positions . 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 moves, which can be easily extended to include moves.
If each move is in the positive direction, our net change in position is steps forward. If we only want to move moves forward, we can go through the configuration of moves in the order , keeping track of our net change of position and, if changing the current move to still allows a net final position increase , 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 .
It is left as an exercise to the reader to prove why we do not need to sort the vectors, leading to an solution.
Time Complexity: or