1779A - Hall of Fame
Author: n0sk1ll
In one move, Milica can replace the whole string with $$$\texttt{AA} \ldots \texttt{A}$$$. In her second move, she can replace a prefix of length $$$k$$$ with $$$\texttt{BB} \ldots \texttt{B}$$$. The process takes no more than $$$2$$$ operations. The question remains — when can we do better?
In the hint section, we showed that the minimum number of operations is $$$0$$$, $$$1$$$, or $$$2$$$. We have $$$3$$$ cases:
No operations are needed if $$$s$$$ already contains $$$k$$$ characters $$$\texttt{B}$$$.
Else, we can use brute force to check if changing some prefix leads to $$$s$$$ having $$$k$$$ $$$\texttt{B}$$$s. If we find such a prefix, we print it as the answer, and use only one operation. There are $$$O(n)$$$ possibilities. implementing them takes $$$O(n)$$$ or $$$O(n^2)$$$ time.
Else, we use the two operations described in the hint section.
A fun fact is that only one operation is enough. Can you prove it?
1779B - MKnez's ConstructiveForces Task
If at some point Milena split $$$a_i$$$ into $$$x$$$ and $$$y$$$, that will not affect the subarray right from $$$a_i$$$ (by splitting some element, it can only get smaller). That means we can try greedy from right to left.
As we described in the hints, we will sort the array greedily from right to left. Let's assume we have sorted subarray $$$[a_{i+1}, \dots, a_n]$$$, and now we are operating on element $$$a_i$$$. Let $$$a_i$$$ be splited into integers $$$x_1,\dots,x_{m+1}$$$ after $$$m$$$ operations. As we want to make all $$$x_i$$$ as large as possible, to spend as few operations as possible on the left subarray, all $$$x_i$$$ will have the value $$$\lfloor \frac{a_i}{m+1} \rfloor$$$ or $$$\lceil \frac{a_i}{m+1} \rceil$$$. So in order to sort the array, $$$\lfloor \frac{a_i}{m+1} \rfloor \leq a_{i+1}$$$ must hold. Therefore, the minimum value of $$$m$$$ for which it is possible to sort the right subarray is $$$\lceil \frac{a_i}{a_{i+1}} \rceil-1$$$. Do not forget to update the value of $$$a_i$$$ to $$$\lfloor \frac{a_i}{m+1} \rfloor$$$ after the transition from $$$a_i$$$ to $$$a_{i-1}$$$.
There are $$$q \leq 2\cdot 10^5$$$ queries: given an index $$$i$$$ and an integer $$$x>1$$$, set $$$a_i := \lceil \frac{a_i}{x} \rceil$$$. For each query, modify the array and print the answer to the original problem. Please note that queries stack.
1779C - Least Prefix Sum
Author: n0sk1ll
Think of the pairs $$$(a_i,b_i)$$$ as segments. How can we visualize the problem?
The pair $$$(a_i,b_i)$$$ represents some segment, and $$$|a_i - b_i|$$$ is its length. Let us try to maximize the sum of the segment's lengths. We present three cases of what a swap does to two arbitrary segments.
Notice how the sum of the lengths increases only in the first case. We want the distance between the first segment's right endpoint and the second segment's left endpoint to be as large as possible. So we choose integers $$$i$$$ and $$$j$$$ such that $$$i \neq j$$$ and $$$a_j - b_i$$$ is maximized. We add twice the value, if it is positive, to the original absolute beauty. If the value is $$$0$$$ or negative, we simply do nothing.
To quickly find the maximum of $$$a_j - b_i$$$ over all $$$i$$$ and $$$j$$$, we simply find the maximum of $$$a_1,a_2,\ldots a_n$$$ and the minimum of $$$b_1,b_2, \ldots b_n$$$. Subtracting the two extremum values produces the desired result.
Solve the problem if $$$a_i \leq b_i$$$ is not guaranteed.
1779D - Boris and His Amazing Haircut
Author: n0sk1ll
The solution does not exist only for "small" $$$k$$$ or when $$$n+m-k$$$ is an odd integer. Try to find a construction otherwise.
Read the hint for the condition of the solution's existence. We present a single construction that solves the problem for each valid $$$k$$$.
One can verify that the pattern holds in general.
How would you write a checker? That is, how would you check that a valid walk of length $$$k+1$$$ exists in the given grid (with all the edges colored beforehand)?