Editorial for DMOPC '21 Contest 3 P3 - Hopping Frogs
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
For convenience, let's assume that all frogs from the first family are red and all frogs from the second family are blue.
Subtask 1
Just by trying a few cases out on paper, we observe that it is always possible to solve the problem by moving the red frog and the family of blue frogs in an alternating fashion.
Subtask 2
This subtask requires some additional experimentation. After solving some small cases where
Subtask 3
First of all, let's calculate a closed form expression for the minimum number of hops required if a solution exists. We first note that for each pair of a red frog and a blue frog, one of the frogs has to jump over the other in order for their relative order to be switched. So, in total, there must be
Now, let's try visualizing the problem by assigning a 2-D coordinate to each arrangement of frogs. Specifically, we map an arrangement of frogs to a point
It is easy to see that there are
- A red frog hops one stone to the right: the number of red frogs to the right of the empty stone increases by
, so this hop moves . - A blue frog hops one stone to the left: the number of blue frogs to the left of the empty stone increases by
, so this hop moves . - A red frog hops over a blue frog: the number of red frogs to the right of the empty stone increases by
and the number of blue frogs to the left of the empty stone decreases by , so this hop moves . - A blue frog hops over a red frog: the number of blue frogs to the left of the empty stone increases by
and the number of red frogs to the right of the empty stone decreases by , so this hop moves .
Visually, we can either move right, up, down-right, or up-left. It is not hard to see at this point that any path in this grid going from
Now we can easily write a program that constructs one of these paths to solve the problem!
Comments