EGOI 2026 Editorial - Seating Plan
Task Authors: Tomohisa Hachiya, Takuma Masuda, Yuki Tanaka
Distances instead of people counts
Most of the calculations we'll need to make are nicer if instead of queries that return people
counts in given ranges we consider each seat a point with an integer coordinate and have
queries that return the distances between those points -- i.e., a straight difference between
the max and min of the three given coordinates. Therefore we'll immediately subtract 1
from each answer to the original query and we'll work with those values instead. The smallest
possible distance is therefore 2 and the largest one is
Brute force
Clearly, for
- The two guests who are involved in all questions with answer
are the ones sitting on the ends. - We can arbitrarily assign one of them to seat
and the other to seat . - There is exactly one question that involves the guest in seat
and has the answer . - From this question we know the two guests
and in seats and but we don't know which is which. - The question with guest
, guest in seat , and any unused guest will tell us whether is in seat or . - Once we know the guests in seats
and , we can use all questions that involve them + a third guest to place everyone else.
Of course, in terms of having the simplest possible implementation, in the
For
Second subtask
We can start by making all queries of the form
One easy way to continue is to take
One general direction of thought: partial solutions
A partial solution will be a sorted collection of guests for whom we already know
their relative positions (that is, up to shifting all of them by the same amount and/or flipping their order).
For example, the partial solution
Below we will see multiple ways to get a good solution. One of them will be based on finding efficient ways to do two things: to construct small partial solutions and to merge them.
Partial solutions via brute force
The brute force approach above can be generalized to an algorithm where we pick some specific small
group of
For example, we can get a partial solution for any
Adding one guest to a partial solution
Given a partial solution and one new guest, we can easily add the guest to the partial solution in
two questions with very little casework. Without loss of generality, let the partial solution
consist of guests
This gives us our first full solution: start with
Merging two partial solutions (slow-ish)
Another useful observation is that when merging two partial solutions into one, we only need a little extra information. In particular, in order to merge the two partial solutions it is sufficient to take any two guests from one partial solution and determine their positions within the other.
We can do that by applying the algorithm from the previous section twice. This gives us an easy way to merge any two partial solutions by asking four additional questions.
And that gives us another full solution: We can divide the
Doing the same thing more efficiently
By more careful casework it is always possible to construct the partial solution for 5 guests
by asking just
Having just the first improvement brings the number of questions down to
Five guests in six queries
If we take four guests and ask all four possible questions about them, we are able to determine almost everything about them. There are always exactly two partial solutions consistent with the answers we got, and their only difference is that the identities of the middle two guests are swapped.
Without loss of generality, suppose that one of these two partial solutions has the four guests in order 0, 1, 2, 3
and let their relative coordinates, left-to-right, be
We will now add a fifth guest (guest 4) and we will make two additional queries:
The new guest sits outside the range of the other four if and only if at least one of the two new distances is bigger than the distance between 0 and 3. When this happens, the bigger new distance tells us the exact position of guest 4, and then the smaller new distance tells us the coordinate of guest 1, which disambiguates between guests 1 and 2.
If we got here, we know that guest 4's coordinate is somewhere in the range
Each of the four cases is handled in the same way. For example, if the query
Note that it is not possible for both new queries to match some of the original distances.
In particular, the two new queries cannot return distances
Faster merging
We omit the details to make this writeup at least a bit shorter. There's nothing interesting, just more careful casework than in the four-query version. In one solution the first step is to take the two points at one end of the shorter partial solution and one point at an end of the longer partial solution. The second step is to show that a good second query can always be made afterwards.
Solutions based on endpoints
It is possible to solve the task using many different strategies. One type of strategies worth mentioning is that we can focus on identifying some guests who sit close to the edge of the row, and then use questions that involve those guests to find unique placement for everybody.
For example, start with all questions of the form
(This solution has some additional special cases that need to be addressed. For example, what should we
do when the distance between guests
Solutions based on a pivot
Suppose we have two guests
What happens if we have such a pair and we query all other guests? If the distance
The key observations here are:
- Each pair can be disambiguated using a single additional query: if we determine the location for one of the guests, the other must simply be in the other possible location they share.
- All individual guests can be resolved quickly together because it's enough to resolve the
outermost guest -- all others must be on the same side of
so that there are no gaps in the seating order.
This approach will give us a new best solution as follows:
-
Use any known approach to find the partial solution for roughly
guests. -
Pick the closest two among them as
and = the pivot. Note that their distance is at most . -
Ask the queries with the pivot for all other guests. This will require
queries. -
Resolve guests that now have a fixed location (the other was occupied by a known guest).
-
For each of the roughly
guests inside ask another query to find their location. -
For each of the (at most slightly fewer than
) pairs of guests around the pivot ask one question to learn who's where. -
Resolve the individual guests left over.
This solution requires
For
Another primitive operation: two pivots, two guests, three queries.
Suppose we already have two pivots
We can now take two new unknown guests
For now, let's assume that both queries returned a value greater than the respective distance, that
is,
The important new observation is that in this setting we can always find the correct locations for
both
Let the possible positions for
If the correct positions are
We'll omit the details and just note that the general idea is to consider each pair of distances
separately and show that they can only be equal for at most one of the four points
In code we don't need the details of this proof. Instead, we can simply iterate over all four candidates and pick one for whom the three answers are distinct.
One possible solution based on two pivots
Start by finding the relative positions of five guests. Take first four of them as the two
initial pivots
Whenever the guest falls inside the pivot's interval, make another query with the same guest + the other pivot to find this guest's location. The guest now divides the pivot into two intervals. Take the shorter of them as the new pivot, and stay with that pivot for the next query.
Whenever you have two queries that match the previous section, make a third query to localize that guest.
As we are at least halving the pivot's interval each time a guest falls inside, there will be
at most
The remaining
in guaranteed queries -- overview
In next section we will write a complete constructive proof that
- Find the relative locations of 15 out of the 40 guests.
- Among those 15, pick two pivots. These will be pairs of adjacent or almost-adjacent guests.
- For each of the other 25 guests ask the query with this guest + one of the pivots.
- For each of those 25 guest we now have two possible positions.
- Use logic to resolve some of the cases that can occur without asking any queries.
- Realize that we can sometimes place not just two but four guests in a single query, as long as some specific conditions are met.
- Make such queries while you can and then resolve the rest by using a clever binary search.
That's it for the overview. If you want all the gory details, brace for impact and start reading on. You've been warned :)
Full writeup: in guaranteed queries
We'll start by using the two building blocks derived above: we can use
The sum of the 14 distances between adjacent elements is at most 39. We can now easily show that the two smallest among those distances must both be smaller than 3. This means that in each of these pairs we have either two adjacent guests, or two guests with just a single unknown guest between them.
Let's label the guests forming these two pairs
In the second phase we will spend one query for each other element
We will alternate between the two pivots in such a way that the final counts of non-special elements for each pivot differ by at most one.
We are at 47 queries asked, and the worst case that can happen is that both pivots were formed
by two adjacent guests, and we now have 13 guests with two possible locations symmetric around
Some of these guests will come in pairs that have the same distance from the same pivot. Each such pair has exactly two possible configurations, and also enforces that no other element can be in either of those locations. We like these, because they are basically just two additional known positions. We'll treat their positions as known and we'll only resolve them at the very end.
Note that at this point unresolved guests who share the same pivot have mutually disjoint pairs of possible positions.
At this point we can apply several greedy rules to resolve stuff without asking any queries:
- All positions between the smallest and the largest currently known position must be occupied. If there is exactly one guest that can be there, place them there.
- We can maintain an interval of positions that may be occupied. We can initialize it to
the interval that starts at
(known positions) and ends at (known positions) . - Whenever we see a position within that interval that cannot be occupied, we can shrink the interval.
- Whenever we have a guest whose one possible position lies outside the possible interval, we place them at the other position.
Once none of these greedy rules apply, we are left with a contiguous range of positions, each with either a known guest or with at least one guest who can be there.
Let's model the situation as a graph. The integer coordinates that can still be occupied by someone will be the vertices and each unresolved guest will be an edge between their two possible vertices. We will color the edges red if they used the first pivot and blue if they used the second pivot.
As the remaining guests who share the same pivot don't share the same locations, each vertex has at most one incident edge of each color. Thus, each connected component must be either an alternating path (possibly consisting of just one edge) or an alternating cycle.
Alternating cycles are impossible. To note why, first observe that the two pivots have distinct
centers and therefore distinct sums of coordinates. For any guest, the sum of coordinates of their
two options matches the sum of their pivot. If we had an alternating cycle, we could count as
follows: Look at just the
Thus, each component is an alternating path. Each of the
From each alternating path, the one unused vertex only has two options: it can only be either the leftmost or the rightmost of its vertices -- otherwise we would have a gap somewhere inside the segment of used vertices.
At this point we can add a new greedy rule: if there is an alternating path with one end inside the segment of the currently known positions, that end must be occupied, and thus its opposite end isn't, and we can resolve it without any queries.
Once none of the greedy rules including the new one apply, we know that each unresolved alternating path has its leftmost vertex to the left and its rightmost vertex to the right of all known positions.
Additionally, one of the ends (i.e., degree-1 vertices) of the alternating path will always be on its outside (i.e., it will be either the leftmost or the rightmost of its vertices). Thus, we can resolve the entire path by resolving the guest who can sit at this vertex. If the guest is there, the other extremal vertex must be empty and vice versa. (There's a cute proof similar to the one for cycles. Note that if we take any point, mirror it around one pivot and then around the other pivot, we have just moved it by twice the distance of their centers. Thus, if we number the vertices along the path, the coordinates of all odd-numbered vertices will form one arithmetic progression and the coordinates of all even-numbered vertices will form another. Our claim then follows easily.)
Now we are ready to resolve everything. The only remaining question is: what is the smallest number of questions in which it's always doable?
We can easily resolve one guest using one question. However, we can also resolve a red guest and a blue guest (i.e., two guests who were queried with different pivots) together using just a single query: we can always pick one of the guests with a known position such that the four possible answers all give different answers. (This is a non-trivial statement and we are omitting the proof intentionally because this text is already way too long.)
There are two things to resolve: if there are pairs of guests who have the same two possible locations, we need to tell which is which, and we need to find which 40 contiguous positions are actually used -- i.e., for each path, which one of its endpoints is unused.
For each path, label its left endpoint dark blue if it can be resolved by querying a blue guest, or dark red otherwise. We can precisely determine the set of used positions by finding the leftmost used dark blue and the leftmost used dark red vertex. Each of these questions can be resolved by binary search. E.g., if we have seven dark red locations, we just need to query three red guests.
We will do two processes in parallel. One process is that we want to resolve all blue pairs and then binary search the dark blue positions, the other process is the same in red. In each step we take the next blue guest from one process, the next red guest from the other process and spend one query to resolve both of them together.
We can easily verify (e.g., by not being smart and just looking at all possible cases) that regardless of how many blue and red pairs there are, in the worst case (which occurs when all unpaired edges are length-1 alternating paths that still survived) we will only need 7 queries to resolve everything. Thus we are done after at most 47+7 = 54 queries, q.e.d.
General solution in roughly 5N/4 queries
This approach generalizes to an (
One possible implementation along these lines looks as follows:
- Use a slower solution that incrementally constructs a partial solution for roughly the first
guests. - In the obtained partial solution, pick two adjacent pairs of guests with smallest distances as the two pivots.
- Proceed as above, with the small differenence that now there can be
moments when the queried guest falls into the pivot's interval. In each of these we simply ask one more question with that guest and the other pivot to place them, and then we continue with the same pivot and the next unknown guest.
We think we know how to improve the
Lower bound
In this section we'll look at the problem from the opposite side: can we guarantee that
all solutions must ask at least some number of questions in order to be correct?
Sure we can. An easy lower bound is around
When solving a test case we need to determine which one out of the
Each query returns one of only
The ratio between those two amounts tells us a lower bound on the number of questions any solution needs to make in the worst case.
If you aren't comfortable working with information, the same argument can be phrased as a purely
counting argument. E.g., assume that you have a deterministic solution that can correctly solve
any input of size
As each of the queries has at most
On the other hand, we know that there are
And therefore for any deterministic solution the number
This estimate tells us that we need at least
Of course, this is not the true optimal number of questions, the lower bound is not tight.
Such a low number of questions would require each question to basically exactly split
the space of remaining valid answers into
Optimality?
So, where's the boundary? What is the optimal solution? Is there a solution that always needs
at most
We had multiple solutions where we could prove that in the expected case the number of
questions they need is
The best solutions the SC had did solve all the prepared test cases and antagonistic strategies for