Thoughts
I have begun training for WF, and want my practice to take a more structured form. Hence this recapitulation, and any others that may follow.
I wasn't as fast as I could have been today. The WA on D was also pointless. It could have been easily avoided by looking at my code for 2 seconds before submitting.
Solutions
A
Reversing the identity sequence clearly works.
B
Let's say that the optimal sequence of operations results in us stopping at position
Then it's optimal to have sat at all chairs
The optimal score for every stopping point can therefore be computed by iterating over std::set or a range query DS.
C1
Notice that the condition:
for all
is equivalent to
for all
Now, for a particular
We notice that
If
Otherwise, we have
- We cannot set
to anything less than , as we have shown to be the smallest valid value of . - We cannot set
to any other multiple of as .
Therefore, we are forced to set
C2
The harder version decouples
This only affects our previous analysis at one place: previously, when we had
But now, we can have
Lemma 1:
There exists an optimal solution wherein the chosen
for all at which we set ( ) is prime.
Proof: easy enough to show, but left as an exercise to the reader.
Lemma 2:
There exists an optimal solution wherein the set of used
is a subset of the first primes.
Proof: left as an exercise to the reader (hint:
We can therefore do a dynamic programming with the following state:
= maximum number of moves we can make in the prefix , if we make no move at position . = maximum number of moves we can make in the prefix , if we set (where is the -th smallest prime).
Computing all of these states takes
D
Firstly, let
Lemma 1:
is a necessary requirement for and to be interconvertible, as after any operation that converts to
Proof: Exercise for the reader (hint: this outer section can never be modified by any operation).
If
Now, consider the RBS trees
The equivalent problem on these trees is the following:
You can perform any number of the following operations on either tree:
Select nodes
and such that neither is an ancestor of the either, and attach some "adjacent" children of (in the ordered child sequence) to the parent of , and attach to . Can the two trees be made isomorphic?
Now, notice the following invariant: The number of leaf nodes remains the same after every operation.
While obviously necessary, it turns out that having an equal number of leaf nodes is sufficient for two trees to be interconvertible.
Proof:
Let's say a tree has
Regardless of the initial structure of the tree, we can always convert it to the following equivalence class representative:
A tree with