Editorial for EGOI '23 P5 - Carnival General
Submitting an official solution before solving the problem yourself is a bannable offence.
Problem author: Nils Gustafsson.
Subtasks may not be ordered for ease of discussion.
The Problem
There are generals numbered
which you want to arrange in a
row. The
-th general ranks all generals
such that
, and it is forbidden
for two generals
and
(
) to stand next to each other in the row if general
is strictly in the second half of general
's ranking.
Construct a way to arrange the generals in a row such that the restrictions
are satisfied. It is guaranteed that there is a solution.
Subtask 3
In general, if one doesn't have the idea for the full solution, it is always nice to start with brute force.
It is easy to see that there are ways to order the generals.
When
,
. Therefore one can simply brute force all the possible
orders and do checking. This brute force can be implemented recursively, or by
iterating through all permutations (for example by using
next_permutation
in
C++).
Note that when we say general and general
cannot stand next to each other,
that means general
cannot be directly to the left of general
, and vice versa.
Subtask 1
Maybe we can start with some smaller and work our way to larger
. Let's
just run the brute force written in subtask 3.
Let's say the brute force returns the lexicographically smallest solution. Then,
for different you should get the following constructions:
:
0 1
:
0 1 2
:
0 1 2 3
:
0 1 2 3 4
It seems that the solution to a certain is
. It is easy to see why
it works, as
and hence generals
and
can always stand next to
each other.
Subtask 2
Let's use the brute force again.
:
0 1
:
1 0 2
:
1 3 0 2
:
2 0 3 1 4
:
2 5 0 3 1 4
:
3 0 4 1 5 2 6
:
3 7 0 4 1 5 2 6
It seems that the solution to odd is:
- Arrange generals
in that order
- Put general
in between each pair of adjacent generals
The difference of the numbers of adjacent generals are either or
.
Since
, each general favors generals with smaller numbers. Hence it is
easy to see that a difference of
or
does not violate the constraints.
For even , simply add general
in between the first two generals, which
are general
and general
respectively. It is also easy to see that adding
general
this way does not violate the constraints.
Full Task
Induction
We can think of the construction in Subtask 1 in another way: start with the
construction for and add general
somewhere in the row to find the
correct construction for
. This induction-like technique turns out to
be useful in solving the full task.
Proving the Existence of a Solution
Note that if there is a way to insert general in a row containing generals
currently (regardless of how they are arranged), and there is a
construction for
, then there is a construction for all valid inputs. This
is because one can get the construction for
easily from
, by
adding general
into the current row.
Now, how many ways are there to insert general ? Since there are
generals
in the row currently, there must be
ways that general
can be inserted.
Additionally,
of the existing generals can be placed next to general
,
while the rest cannot. Let's call the generals that can be placed next to general
"good" generals, and the rest "bad" generals.
We want to find one of the following:
- A pair of adjacent generals such that both of them are good (condition 1)
- Either the first or the last general that is good (condition 2)
Now, assume the contrary that neither of these two conditions are true. Then,
the first and last generals are bad, and between every pair of good generals there
is at least one bad general. There are good generals, so we need at least
bad generals for this to occur. But we also know that the number of
bad generals equals
, and
.
We found a contradiction, hence the assumption that "neither of the two conditions
are true" is false. Therefore, at least one of the conditions is true, and
that means we can always find a way to insert general .
Constructing the Solution
Start with generals, then add each new general by exhausting each possible
position the new general can be in.
The time complexity is .
Aftermath
Here are some questions you may think about after attempting this task, if you are interested:
- If we instead give
constraints in the form
, such that
and
cannot stand next to each other, where
, is the general problem solvable in polynomial time? Alternatively, can you reduce it to a standard NP-complete problem?
- What if we impose additional constraints on the arrangement of the generals, such as requiring that certain pairs of generals must stand next to each other or that certain generals must be placed in specific positions?
Comments