Note: I can't figure out how to place the tutorials inside spoilers. If someone is familiar with how CF spoilers work and can help I would really appreciate it. For now, be warned that the tutorials are visible by default (but everything else isn't)
UPD: I figured out how to use spoilers! Also added implementations for all problems.
Thanks for participating in our contest!
1509A - Average Height
Author: Kuroni
First solve: Nutella3001 at 00:01:02
HintHow can you write the condition that $$$\frac{a_u + a_v}{2}$$$ is an integer in a more useful way? Think of the parities.
Comments from the authors Implementation
1509B - TMT Document
Author: Ari
First solve: blue at 00:04:51
HintYou can think of the partition as associating to each $$$M$$$ character a $$$T$$$ to its left and a $$$T$$$ to its right. What's the best way to perform this assignment?
AnswerAssign greedily from left to right.
Implementation
1509C - The Sports Festival
Author: Ari
First solve: Dukkha at 00:05:32
Hint 1What is the discrepancy of the last stage? How can we make it smaller in previous stages?
AnswerThe discrepancy of the last stage is always the difference between the largest and the smallest $$$a_i$$$. We can make it smaller in the previous stages by placing the smallest or the largest $$$a_i$$$ at the end.
Comments from the authorsThis problem was originally going to appear in Codeforces Round 668 (Div. 2), but it was removed one day before the contest for balance reasons :P
Implementation
1508A - Binary Literature
Author: Ari
First solve (Div. 2): traxex2 at 00:22:48
First solve (Div. 1): tourist at 00:05:04
Hint 1Consider two of the strings. We can obviously achieve our goal using at most $$$4n$$$ characters. How can we save some of them?
AnswerTake advantage of any common subsequence in our two strings by including the characters in it only once.
Hint 2Find a long common subsequence that has a simple structure. You'll need to involve the third string as well.
Hint 3Either $$$00\dots 0$$$ or $$$11\dots 1$$$ is a subsequence of each string.
Comments from the authorsThis problem was originally suggested as a 2B. It turned out to be too hard and was switched to 2C, before being further switched to 1A because it was still too hard. Some testers still believed this problem to be even harder, but we ended up deciding to have it as 1A with a larger score than usual.
Apologies to the testers who had to solve this as if it was the second easiest problem in the contest :^)
Implementation
1508B - Almost Sorted
Author: Both of us!
First solve (Div. 2): shenmadongdong.qaq at 00:29:52
First solve (Div. 1): tourist at 00:08:19
Hint 1What is the general structure of an almost sorted permutation?
AnswerStart with the identity permutation, choose some disjoint subarrays, and reverse each of them.
Hint 2How many almost sorted permutations of length $$$n$$$ exist? In other words, how many ways are there to reverse subarrays?
Hint 3Two options — either solve recursively in a greedy fashion or find a smart bijection.
Comments from the authorsThis problem's creation has a funny story. Back when we were coming up with problems for Round 668, I (Ari) came up the following relatively simple and standard problem and shared it.
The next day, Kuroni saw the problem, but misread it and ended up solving the version that made it into this contest, which we think is a lot cooler!
One more fun fact for your consideration: This problem's name in Polygon is "sorrow".
Implementation
1508C - Complete the MST
Author: Kuroni
First solve (Div. 2): deepspacewaifu at 01:42:36
First solve (Div. 1): maroonrk at 00:33:17
Hint 1How can we solve this problem without the restriction that the xor of all edge weights is $$$0$$$?
AnswerJust assign $$$0$$$ to every unassigned edge :P
Hint 2When does the solution to the unrestricted problem fail for the real problem?
AnswerWhen any minimum spanning tree (of the graph with every unassigned edge having weight $$$0$$$) contains every unassigned edge, and the xor sum of all the edge weights is not $$$0$$$.
Hint 3Leave the xor sum of the weights of the unassigned edges in the spanning tree fixed. How do we minimize their sum?
AnswerAssign the entire xor sum to a single edge, and assign weight $$$0$$$ to every other edge.
Hint 4Exactly one unassigned edge ends up with a non-zero weight. If we don't use all unassigned edges, which others can we use?
AnswerAny edge that isn't part of the MST when we consider unassigned edges to have weight $$$0$$$, and that doesn't form a cycle with pre-assigned edges with smaller weights.
Comments from the authorsIf the unassigned edges in the graph don't form a forest, then the answer is simply the weight of the MST where all the unassigned edges have weight $$$0$$$. Thus the interesting tests in this problem require these edges to form a forest, which means $$$m \ge \frac{n(n - 1)}{2} - (n - 1)$$$. This automatically limits the maximum value of $$$n$$$ to approximately $$$2\sqrt{m}$$$. Concretely, $$$n \le 633$$$ for all interesting tests in this problem. You can also use this to obtain a solution that runs in $$$O(m \sqrt{m})$$$, which a tester found, and which passes if implemented reasonably.
Implementation
1508D - Swap Pass
Author: Ari
First solve: ksun48 at 00:57:13
Hint 1It is always possible to find the sequence.
Hint 2There's a simple algorithm that fixes a point and repeatedly moves its current label to the correct position. When does it work?
AnswerWhen the permutation of the labels is a single cycle.
Hint 3Any permutation can be split into one or more cycles. What can we do to these cycles?
AnswerMerge them, by swapping two elements in different cycles.
Hint 4We would like to merge all cycles of the permutation by swapping elements in different cycles and then repeatedly move each label from a certain point to its correct location. How can we do this without intersections?
AnswerFix a point beforehand to perform the final step, and sort angularly with respect to this point.
Comments from the authorsThis problem was originally proposed with the points always being the vertices of a convex polygon. This allows us to do a slightly different solution where we first merge the cycles and choose the pivot afterwards by choosing a point unaffected by the previous swaps.
The reason for having $$$n = 2000$$$ in this problem rather than a larger $$$n$$$ like $$$10^5$$$ is because of the validator: it is essential to not have three collinear points in this problem, and I don't know how to check this for large $$$n$$$. The small $$$n$$$ also makes the checker much easier to write, though it's possible (albeit tricky) to write a checker that works in $$$O(n \log n)$$$.
Regarding the number of operations used: The solution described in the editorial uses approximately $$$\frac{3}{2}n$$$ operations in the worst case when the permutation consists of cycles of size $$$2$$$. The minimum number of operations is lower bounded by $$$n - 1$$$ by combinatorics reasons when the permutation is a single cycle. Moreover, by Euler's formula, we have an upper bound of $$$3n - 6$$$ operations for any valid sequence, which goes down to $$$2n - 3$$$ when the points are the vertices of a convex polygon. I don't know of a solution that uses $$$cn$$$ operations for some $$$c < 3/2$$$, nor do I know of a general test case where it can be proven that at least $$$cn$$$ operations are needed for some $$$c > 1$$$, so I'd be really interested in seeing either.
Implementation
1508E - Tree Calendar
Author: Kuroni
First solve: ecnerwala at 01:02:26
Hint 1During the process, we will repeatedly push the same label down until we no longer can. What happens at this point?
AnswerThe labels that have been fully pushed down will look like a post-order of the tree, while the other labels will look like a pre-order of the tree.
Hint 2Look at the children of a particular vertex. What can we say about the ones that are fully pushed down in relation to the ones that aren't?
AnswerEvery fully pushed down label is smaller than every other label.
Hint 3For each node, the order of the labels of its children stays fixed.
Hint 4How can we use the previous observations to identify the state of the process?
AnswerWe can reconstruct the DFS order and the fully pushed down labels. Then check that the current pushing step of the process is valid.
Comments from the authorsThis problem was inspired by a wrong solution to 1477D - Nezzar and Hidden Permutations, but it seems that knowing that problem beforehand doesn't help at all to solve this one.
Implementation
1508F - Optimal Encoding
Author: Kuroni
First solve: ecnerwala at 01:52:02 (The only contestant who solved this problem!)
Hint 1Start by solving the problem for $$$q$$$-encodings only.
Hint 2We can get an encoding by including every possible edge. Which of them can we exclude?
AnswerWe need to keep an edge $$$u \to v$$$ if and only if no other path from $$$u$$$ to $$$v$$$ exists.
Hint 3What does the previous observation look like from the perspective of a single vertex?
AnswerEach vertex has at most two outgoing edges, one on each side, and we can easily characterize when one of them is redundant.
Hint 4We need to solve the full version now. For a vertex $$$u$$$, how do the endpoints of its outgoing edges change when we add a new interval?
AnswerThe endpoints of the right edges increase monotonically, while the endpoints of the left edges decrease monotonically.
Hint 5How can we use the previous observation to characterize edges becoming redundant more concretely?
AnswerFor each right edge, we can find a particular left edge, such that the right edge becomes redundant once the left edge appears. This gives us an interval of times for each edge during which it is relevant.
Hint 6Use Mo's algorithm on the input intervals to find the relevant edges.
Comments from the authorsHuge shoutouts to tfg who came up with one of the key steps of the solution! Including this problem probably would have not been possible without him. ❤️
Implementation
Finally, here's some funny moments that happened while we were working on the round :)