EGOI 2026 Editorial - Ovenmasters
Task Author: Tomohisa Hachiya, Yuki Tanaka
Let us call the pizza currently lying on a table the top pizza of that table.
After the first
The first useful observation is about a single table. Whenever a baker replaces a
pizza on a table, her rank must be larger than the rank of the pizza she ate.
Therefore, in every stack, the ranks have to be increasing from bottom to top.
If this is not true, the answer is immediately
The first
Subtask:
With one table, the process is very simple. The stack must be increasing. Any baker not in the stack has to leave, which is possible only after the current top pizza has become larger than that baker's rank.
Thus we can start with the first stack value and then repeatedly take the smallest rank that is currently possible:
- either the next value in the stack, which replaces the current top;
- or a missing value smaller than the current top, which leaves.
If at the end some missing value is larger than the final top pizza, it could never have left, so the instance is impossible.
Subtask: , , and no baker leaves
In this subtask every baker appears in one of the two stacks. After the first two arrivals, the next arrival must therefore be the next unused value of one of the two stacks.
Let table
Now consider a possible next arrival with rank
- If
, then table contains a pizza better than , while table does not. So must go to table . - If
, then both tables contain a pizza better than , and the worse of those two pizzas is . So must go to table . - If
, then there is no better pizza for . This case cannot happen in this subtask, because no baker leaves.
Therefore, to decide whether the next unused value of a stack can be taken now, we just check whether it falls into the interval for that stack.
At each step there are at most two candidates: the next unused value of the first
stack and the next unused value of the second stack. We test which of them are
currently possible and append the smaller possible one to the answer. If neither
candidate is possible, the answer is
This greedy choice gives the lexicographically smallest order, because every valid order with the current prefix must choose one of these possible candidates next. The smaller one is therefore always best.
The implementation can be completely direct and runs in
Keeping the tables sorted
Suppose the current top ranks of the tables, sorted increasingly, are
A baker with rank
where for the last table there is no upper bound. This is because
In particular, if table
It is also convenient to add one fake table for the bakers who leave. Its current
value is
which means that no real table has a better pizza for her.
Subtask: and no baker leaves
For more than two tables, the same idea still works. Every baker appears in some stack, so the next arrival must be the first unused value of one of the stacks.
We keep the current top value of every table. Repeatedly look at the next unused
value of each stack, check whether that value would currently choose this table,
and append the smallest value that passes the check. If no stack has a possible
next value, the answer is
The lexicographic argument is the same as above: after a fixed prefix, we choose
the smallest value that can legally be the next arrival. A straightforward
implementation is fast enough for
Subtask:
For larger
First sort the real tables by their first stack value. We also add the fake
table for bakers who leave. After sorting, think of these as rows
Each row
- a current value
, initially its first value ( for the fake row); - a list of remaining values that still have to be processed in increasing order.
For a real row, the remaining values are the rest of its stack. For the fake row, they are all ranks that do not appear in any stack, sorted increasingly.
The values
For row
When can we process
with no upper bound when
Now scan the rows from left to right and find the first row whose
After we find this row, append
This takes
Full solution
The full solution is the same row-based algorithm. The only extra care needed is
to avoid starting the scan again from row
Build
- one fake row containing
followed by all ranks that do not appear in the input; - one row for each table stack.
Before sorting the rows, save the first
Then sort all rows by their first value. The fake row, whose first value is
Now maintain an index
- If the current row has no remaining values, move to the next row.
- Otherwise let
be the first remaining value in this row. - If
, the row is not increasing, so output . - If this is the last row, or
, then is the smallest possible next arrival. Append it to the answer, set , remove it from the row, and move one row left if possible. - Otherwise,
is still too large: it would currently choose a later table. Move one row right.
If the scan finishes and the answer contains all
Correctness proof
We prove that the algorithm outputs
First, each real stack has to be increasing. Every time a baker appears in a stack, she replaced a better pizza, hence her rank is larger than the previous top of that table. Therefore a non-increasing stack is impossible, and the algorithm correctly rejects it.
Second, once the real tables are sorted by their current top ranks, their order
never changes. If a baker with rank
Third, the fake row exactly represents bakers who leave. A missing rank
Now consider any moment during the algorithm. In row
Among all possible next values, the smallest one is in the leftmost possible
row: every possible value in row
Thus, whenever the algorithm processes a value, it appends the smallest rank that can appear next in any valid continuation. This is exactly the greedy choice needed for lexicographic minimality. If the algorithm cannot process any remaining value, then no valid next arrival exists, so no valid order exists.
Therefore, if the algorithm outputs
Complexity
Sorting the rows and the missing ranks takes
The memory usage is