Editorial for EGOI '23 P8 - Guessing Game
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Problem author: Edward Xiao.
Solution 1:
(10 points)
On the first houses, Anna draws the numbers
in order
of visiting. The number Emma draws must also be one of
, and
result in a duplicate value, and it will be the only duplicate value since all the
other values are unique. Thus, we find the pair of houses that contains this
duplicate value and guess those two houses.
Solution 2:
(30 points)
Split the houses into two halves. For each half, we perform the same strategy as above. If either half contains a duplicate, we guess those. Otherwise, we guess the largest number in each half, since these were the last ones to be picked.
Solution 3:
(36 points)
Note that we can actually improve on the strategy above by splitting the houses
into thirds. For each third, repeat the strategy from for all numbers
except the last. For the last houses of the blocks, we pick two values
and
.
Then, the strategy is simple again: if there are duplicates of
or
, guess those
and return. Otherwise, there must be duplicates of the smaller values in one of
the thirds, so we will guess those.
Solution 4:
(60 points)
We can generalize the previous solution even further by splitting the houses into
blocks and picking a set
of
numbers for each number that
is not the last in its block. For the last number in each of the
blocks, we
assign them a different set
of
numbers. If there is a duplicate of
an element in
, we guess those. Otherwise, there must be a duplicate of an
element in
in one of the blocks, so we guess those instead.
Solution 5:
(90 points)
For 90 points, we will use divide and conquer to split the houses into multiple
layers of blocks instead of just one layer. Consider a segment tree layout over
the houses. For each index, we say it "completes" a segment in the segment
tree if it is the last index in that segment to be assigned a value. In each round,
Anna picks the lowest depth of any segment that
completes. For Bertil,
consider the values written on the doors assuming Anna picks the last value
according to their strategy. In this case, Bertil can just find
and return its
index. However, if there is no
in the array, then there are two cases:
- The
was replaced by a
. Then there are exactly two houses with
s written on them, and we know that one of them must be Anna's, since there should only be one
if the strategy was followed for all indices. This is because the index that was picked later not only completes the segment on layer
, but also necessarily completes the segment on layer
since the other half is already completed.
- The
was replaced by a value greater than
. Then, locate the half that contains the only
in the array. We know Anna's house must not be in this half, since it was completed first. Thus, we may recursively solve the problem on the other half, completing the solution.
Note that since is never actually picked by Anna, we can subtract
from all
values Anna picks to ensure we have exactly
.
For illustrative purposes, consider an example where , Emma's house
has index
, and Emma and Anna visit the houses in the following order:
. Then, after Emma writes a number
on her own house,
the array
will be
. We run through Bertil's strategy in the
3 possible scenarios:
: since there are duplicate
s, it is clear that one of these must be for Emma's house. Guess
and return.
: since there is a unique
, Emma's house must be in the half of the array that does not contain
. Now we get to
. Repeat our logic recursively. Since there are duplicate
s, it is clear that one of these must be for Emma's house. Guess
and return.
: since there is a unique
, Anna's house must be in the half that does not contain
. Now we get to
. Since there is a unique
, Anna's house must be in the half of the array that does not contain
. Now we get to
. Since there are duplicate
s, it is clear that one of these must be for Anna's house. Guess
and return.
Solution 6:
or
(100 points)
To solve the problem fully, we consider the following two-phase approach:
In the first phase Anna just writes the number on the first
houses which are visited. In the second phase (which we describe later), we
promise that Anna will not write the number
on the remaining
houses.
Now we need to handle two cases:
- If Emma does not write the number
on her house, then we know it is one of the
houses not with a
.
- If Emma writes the number
, then we need to figure out which of the
houses with
written on it belongs to Emma.
In the former case, we have essentially reduced the problem to an instance with
, but now with numbers
.
For the latter case, we make the following observation: If we know the sum
of indices, modulo
, of the
houses not part of the first phase, then
we can figure out Emma's house in case she writes
. Indeed, we can identify
that we are in the second case by counting the number of houses with
written
on them. Then, we know that Emma's house index plus all indices of houses
without a
, must equal
. Hence we can solve for Emma's house index.
The strategy for the second phase is now the following: we want to essentially
solve an instance where while simultaneously encoding the sum
(which is known at the start of the second phase).
One has to be a bit careful on how to encode the sum , in the remaining
houses, since one of these houses we do not have control over and
Emma can write
on it instead. One way of doing it is to write
(modulo
) in binary and then duplicate each bit, to make sure that we can recover the
sum
even after one bit (the one corresponding to Emma's house) is dropped.
Solving the instance on in our strategy can be done in multiple
ways:
- Using Solution 1 (
) for the recursive part, which will end up in ≈ 76 points with
.
- Recursively using our two-phase strategy here, which will result in ≈ 96
points and
.
- Using Solution 5 (
), which happens to be quite good with small
. If implemented carefully this will obtain the full 100 points using
, see below for details.
Detailed optimization tricks for 100 points:
- Use
and
numbers for the first phase (so
).
- In the second phase we run Solution 5 with
, which will use numbers
.
- Instead of encoding the sum
modulo
, we use the fact that we have two guesses and encode it modulo
. This requires
bits of information, since
.
- Note that Anna will write exactly
"
"s in the second phase (see Solution 5 for more details). Hence we can change some of these "
"s to "
"s to encode the sum
.
- All in all, the solution becomes quite succinct: the jury's 100pt solution is just 40 lines of Python code!
Harder Version: 
It is possible to solve the problem with being constant (that is not growing
when
grows). The jury is aware of a solution using
no matter how
large
is. We leave finding a solution with constant
(or even as low as
) as a difficult bonus challenge.
The jury can also prove that using is impossible (even for small
); so
what about
, is it possible or impossible?
How does the grader work?
The grader for guessinggame is a bit complex. It might run Anna's part twice to try to predict what would be a confusing number for Emma to write on her house.
In the grader, the order of houses is fixed per test case, but the value Emma writes on her own house is chosen adaptively. The second line of each .in file explains how the value gets chosen for that test case:
random
: the last line of the input file contains a number. The value is chosen as
.
interact
: same asrandom
, except that the judging is performed interactively, instead of non-interactively for performance. This is an implementation detail which should not matter. Seeincludes/
for more details if curious.copy
: the last line of the input file contains a number. The value is copied from house with index
.
fork
: the last line of the input file contains a number. Phase 1 of the submission is run as a trial run with Emma's house placed at (
-based) position
of the house visit order, and the house that was originally at position
removed. Let the number that Anna writes on that house be
. Then in the real run of phase 1, Emma writes the number
on her house.
That is, Emma essentially asks Anna "if we had visited my house at time
, what number would you have written on it?".
The grading is performed by adding a file that gets compiled/run together with
the submission and uses the fork()
function to make it run multiple times.
Comments