Editorial for EGOI '23 P5 - Carnival General
Submitting an official solution before solving the problem yourself is a bannable offence.
Problem author: Nils Gustafsson.
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 next_permutation
in
C++).
Note that when we say general
Subtask 1
Maybe we can start with some smaller
Let's say the brute force returns the lexicographically smallest solution. Then,
for different
:0 1
:0 1 2
:0 1 2 3
:0 1 2 3 4
It seems that the solution to a certain
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
- Arrange generals
in that order - Put general
in between each pair of adjacent generals
The difference of the numbers of adjacent generals are either
For even
Full Task
Induction
We can think of the construction in Subtask 1 in another way: start with the
construction for
Proving the Existence of a Solution
Note that if there is a way to insert general
Now, how many ways are there to insert general
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
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
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