EGOI 2026 Editorial - Ferris Wheel
Task Author: Nils Gustafsson
In this task, we are given cabins numbered from to .
For each cabin, we know whether the next cabin on the wheel must have a larger
number () or a smaller number (). We must decide whether there exists a
cyclic ordering of all cabins satisfying every requirement, and if so, print one
such ordering.
Key observation: In a valid configuration:
- cabin can never have a sign, because there exists no cabin with a smaller number;
- cabin can never have a sign, because there exists no cabin with a larger number.
Subtask 1:
There are only three cabins: , , and .
Thanks to the key observation, if or , the answer is .
Otherwise, the answer is always . In this case, cabin must have a sign,
cabin must have a sign, and cabin can have either sign. If ,
we can print ; otherwise, we can print .
Subtask 2: Exactly one
Again, cabin cannot have a valid sign. Since there is exactly one '+' sign, the only
possible valid case is that the unique sign is at position .
If this is not true, the answer is . Otherwise, one valid ordering is
In this ordering, cabin is followed by a larger number, and every other cabin
is followed by a smaller number. The last cabin in the ordering is , and it is followed
cyclically by , which is smaller. Thus, the construction satisfies all the conditions.
Subtask 3: Alternating signs
In this subtask, the signs alternate.
For an alternating string that admits a valid configuration, the conditions from the key observation ( and ) determine the whole string. In fact, by examining the cases:
- if is odd, then and the answer is clearly (if they are both then is not valid, otherwise is not valid);
- if is even, then and the answer is if and otherwise (both and are not valid).
In particular, in the only valid case, must be even and the string must be of the form:
One valid solution is then to print the swapped adjacent pairs
and then finish with the pair . Thus the full order is
Let us check the transitions. In each swapped pair , the first cabin
has a sign, so going from to is valid. The second cabin has a
sign, and the next printed cabin is larger, so the transition out of the pair is
also valid. After the last swapped pair, cabin has a sign and is followed
by , which is larger. Then cabin has a sign and is followed by
, which is larger. Finally, cabin has a sign and goes back
cyclically to , which is smaller.
This solves the alternating subtask in time.
Subtask 4 and Full Solution
We now prove that the two necessary conditions from the key observation are
also sufficient: that is, if and , then a valid Ferris wheel
ordering exists.
Assume and . Construct the answer like this:
- Print all indices with in increasing order.
- Then print all indices with in decreasing order.
For example, if
then the cabins are , and the cabins in decreasing order are
, so we may print
Let us check why this always works. Notice that there is always at least one cabin () and at least one cabin ().
In the first part, all cabins except the last one are followed by larger cabins. By construction, the last cabin is followed by , which is strictly larger.
In the second part, all cabins are printed in decreasing order, so each one except the last is followed by a smaller cabin. Since we cyclically return to the first printed cabin at the end, the last cabin is followed by , which is strictly smaller.
This proves both the construction and the characterization of the impossible
cases.
For Subtask 4, we have , so it suffices to maintain one list while
iterating through the cabins and inserting each index either at the beginning or
at the end. Since inserting at the beginning takes linear time, this
implementation runs in .
For the full solution, we need a faster implementation.
One possibility is to store all the cabins into one list, in increasing order, and all cabins into another list, in decreasing order. Then print the list followed by the list.
This can be done in time, by first reading the string and storing the cabins and then reading it backwards and storing the cabins.
Alternative full solution
There is also a different approach, built on Subtask 3. Every string can be
seen as an alternating sequence of blocks of consecutive or .
For example, if
we split it into blocks as follows
and thus group the corresponding cabins like this:
We know how to deal with alternating strings, and we can apply the ordering of
Subtask 3 to the blocks. It only remains to sort the cabins inside each block.
- if the block is made of [], we place the cabins inside it in increasing order;
- if the block is made of [], we place the cabins inside it in decreasing order.
In the example above, first order the numbers according to the blocks in this way
and then applying the construction of Subtask 3 to the alternating blocks gives:
This gives us the following construction:
It can be checked, similarly to the other constructions, that this always works. This construction can also be done in time.