tutorials-2-15
tutorials-2-15
Page 1 of 6
Uni Stage 15: Macau
Cup The 2nd Universal Cup, Dec 23-24, 2023
• For any 1 ≤ j ≤ n, add an edge (ai , aj ) for the largest 1 ≤ i ≤ j such that ai > aj .
• For any 1 ≤ j ≤ n, add an edge (bi , bj ) for the largest 1 ≤ i ≤ j such that bi < bj .
Clearly, this construction of G satisfies all the partial orders with at most 2n−2 edges without introducing
any additional partial orders. Hence, this construction is valid as long as at least one solution exists.
To check if this construction of G is actually valid, we still need to compute the lexicographically
smallest/largest topological ordering of G and compare them with the two arrays. A way of doing so
is to modify Kahn’s algorithm by maintaining all valid vertices in a priority queue, each time choosing
the next vertex greedily. The overall complexity would be O(n log n).
Lemma G.1 If the game is under the zero case, then LP always wins.
The proof to Lemma G.1 is direct since no matter what two numbers are left, LP can always make them
become 0 in the last move. The slightly harder case is the one case.
Lemma G.2 If the game is under the one case, then LP wins if and only if:
• After shrinking all consecutive occurrences of 1 to the 0/1 number matching its parity
(i.e., shrinking an odd number of consecutive 1s into one 1 and even ones into nothing),
the number of 1s is at least the number of 0s.
Lemma G.2 can be easily verified using induction. We briefly discuss how to come up with it besides brute-
forcing the small cases and observing the pattern. Note that LP loses only when the last two numbers are
both 0 in the last move. So LP will try to maximize the difference between 1s and 0s, and SP will try to
minimize it. Normally, one step will change the difference by at most 1 except for the case when merging
two 1s into a 0 using addition. Considering the effect of this case gives the lemma above.
To actually play the game interactively, since the constraint is low, one is allowed to enumerate all possible
choices and compute the winner of the game. This takes O(n3 ) time.
Page 2 of 6
Uni Stage 15: Macau
Cup The 2nd Universal Cup, Dec 23-24, 2023
Lemma H.1 Given any tree T with n vertices and a sequence s ∈ [n]n , s is a parking function
of T if and only if the following holds:
We first prove Lemma H.1. The only-if direction is simple since an insufficient amount of starting cars
in a subtree will obviously leave an empty space in that subtree. To show the if direction, note that a
sufficient amount of starting cars in some subtree means that the root of this subtree will eventually be
occupied. Then, the condition in Lemma H.1 implies that all vertices will be occupied, meaning s is indeed
a parking function.
We remark that Lemma H.1. also reveals that whether some s ∈ [n]n is a parking function or not does
not depend on the order of the elements.
Then, one only needs to consider for each vertex how many times it is present in the function. we can try
to solve the problem using the following dynamic programming: Let dpv,i denote the number of ways to
assign this “occurrence time” and arrange them for each vertex in the subtree of v so they are valid and
sum to i.
Calculating this dynamic programming would naturally take O(n3 ) time. However, note that in this
dynamic programming, for each vertex v, the valid choices of second dimension i can only range from
|SubT (v)| to |SubT (v)| + depT (v), where depT (v) means the depth of v in the tree T . (The depth of the
root is 0). The lower bound is by Lemma H.1, and the upper bound is also easy to observe. (If the amount
of starting cars in the subtree of v exceeds |SubT (v)| + depT (v), surely some car has no place to park
since they can only park in the subtree of v and in the path between the root and v.) Then, this dynamic
programming can be calculated in O(nh2 ) time, where h is the height of the tree.
This is where the condition that the tree is generated randomly comes into play, that the generated tree
with high probability has height O(log n). One can calculate that with probability no more than 10−9
(which we can safely assume will not happen), this random process will generate a tree with a height of
more than 60. Therefore, this problem is solved in O(n log2 n) time.
1. Use the Refresher Orb as long as it’s ready, i.e., after b seconds, gain 160 × b ab c + 1 gold.
2. Use the Refresher Orb after use of the Hand of Midas, i.e., after a×d ab e seconds, gain 160× d ab e + 1
gold.
Page 3 of 6
Uni Stage 15: Macau
Cup The 2nd Universal Cup, Dec 23-24, 2023
second operation (move the hand clockwise) as “deferred”; that is, after performing the first operation at
least once, one has the choice of “go from room i to i + 1 mod n”. Then, the number of vertices in this
new graph becomes n + 1, and the total number of edges becomes 2n − 1 at most.
We just need to construct a set of paths (which we call generating paths) that satisfy the condition “in
the scenario of a square with a side length of 2i , all cells in each square of the i-level division are passed
by the same set of paths, and the four squares of the i-level division in each square of the (i − 1)-level
division have different passing situations” to obtain the i-th group of answer paths that satisfy the original
requirement.
Because the requirements for each generated path group are locally consistent, we can construct them
recursively.
In our construction, each group’s generated paths are symmetric with respect to the diagonal from the
top left to the bottom right. We will only show the first path in each group. The first path of the first
group is shown in the figure below:
Page 4 of 6
Uni Stage 15: Macau
Cup The 2nd Universal Cup, Dec 23-24, 2023
The first path of the second group is shown in the figure below:
It can be noticed that this path not only meets the requirements for generating paths but also can be
connected at both endpoints to form a loop, with the left half occupying the bottom right corner of the
grid. For the first line of the generated path, we generate it based on the first line of the (i−1)-th generated
path. The resulting path still meets the requirements for generating polylines and connects at both ends
to form a loop, with the left half occupying the bottom right corner of the grid. Our generation method
is as follows:
First, connect the paths into a loop, then delete the second-to-last cell in the last row. Now, the loop is
broken into a path, with the two endpoints in the third-to-last cell of the last row and the second-to-last
cell of the second-to-last row. We extend the second-to-last cell of the second-to-last row to the right by
one cell. Now, the two endpoints of this path are in the third-to-last cell of the last row and the last cell
of the second-to-last row. We then perform a reflection with the rightmost side as the axis of symmetry,
followed by a reflection with the bottom side as the axis of symmetry.
Page 5 of 6
Uni Stage 15: Macau
Cup The 2nd Universal Cup, Dec 23-24, 2023
Now, the generated figure is a loop. If it is disconnected from any point, a path that not only meets the
requirements of generating paths but also forms a loop when the two endpoints are connected is obtained.
This is because the square grid at the bottom right is obtained by reflecting the square grid at the top
left twice along the axis, and the area where the path passes through in the top left square grid remains
unchanged. Therefore, the path occupies the left half of the square grid at the bottom right.
The following image shows the first line of the fourth group generated from the first path of the third
group using the same method (connecting the endpoints of the path to form a loop).
The following image shows the full fourth group (connecting the endpoints of the path to form a loop).
Page 6 of 6