## Editorial for DMOPC '21 Contest 3 P3 - Hopping Frogs

**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:

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 , we notice that the frogs always arrange themselves in an alternating pattern of red, blue, red, blue, ... before they jump over each other and reach their destinations. It is thus possible to induct on this pattern in order to obtain a recursive solution, each time adding a single red and blue frog to the two ends of the pattern. Special care should be taken to note the colour of the first frog in the sequence in order to maintain the alternating pattern.

#### 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 moves of the second type. Then, we note that each red frog is stones away from its target, and each blue frog is stones away from its target. Therefore, a total distance of needs to be traveled for all the frogs to reach their destinations. If we were to only make hops of the first type then we would need exactly hops, but each of the hops of the second type travels a distance of instead of , each saving a single hop. Thus, the total number of moves required is . Note that this is also the *only* possible number of moves.

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 if there are red frogs to the right of the empty stone and blue frogs to the left of the empty stone. Then, the initial arrangement of frogs corresponds to , and the desired arrangement corresponds to . So, any valid solution should move our point from to . The diagram below shows a grid of all the possible points:

It is easy to see that there are points in the grid. We may also note that through any sequence of moves, each point is traversed at most once. Given that we need to make exactly hops, it follows that any valid solution must visit every coordinate in the grid exactly once. Now, let's look at how each possible hop changes the coordinates of our point:

- 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 to visiting every point exactly once corresponds to one of the following two patterns:

Now we can easily write a program that constructs one of these paths to solve the problem!

## Comments