Editorial - 2023 Mini Contest 5 (JSOI Selection)
C2351 - Hugo's Garden
Instead of considering the contribution of happiness by placing each of the $K$ sprinklers, we may try to consider the contribution
of placing all $K$ sprinklers at once.
As sprinklers must not share the same row or column, the problem becomes finding the sum of the first $K$ maximal rows and columns.
This problem can be simply solved by sorting the sums of the rows and columns.
C2352 - Bob, King and Rice
Observe that the amount of grains Bob gains by playing the game with a single pile of $k$ grains is the same as the number of '1's
in the binary representation of $k$.
We can compute the number of piles within the given range for every position of '1' seperately with simple math.
C2353 - Robo Race
First construct the path from the first possible start to the last possible end.
It can be observed that each query will follow this path after 0 or 1 vertical movements.
We can maintain an array of the positions $P$ where the Robo is blocked by an obstacle and has to move up/down.
For each query, figure out the first obstacle on the original path that Robo is blocked by.
Then, a simple binary search on $P$ and some special handling would give the answer.
C2354 - Bicolored Blocky Buildings
The obvious upper bound of $H$ is $\lfloor{\frac{\sum_{i=1}^n A_i}{N-1}}\rfloor$.
Let us try to construct a solution with this maximum $H$.
Observation 1 : $min(A_i)_{1\le i\le N} \lt H$
Let us prove by contradiction:
If $A_{min} \geq H, \sum_{i=1}^N A_i \geq H\times N$.
However, we know that $\sum_{i=1}^N A_i \lt H\times N$.
$\therefore A_{min} \lt H$
Observation 2 : $A_{min} + A_{max} \geq H$
Let us prove by contradiction:
If $A_{min} + A_{max} \leq H-1, A_{max} \leq H-1-A_{min}$
$\sum_{i=1}^N A_i \leq (H-1-A_{min}) \times (N-1) + A_{min} = H(N-1) - (N-1) - (N-2)\times A_{min}$
However, we know that $\sum_{i=1}^N A_i \geq H(N-1) > H(N-1) - (N-1) - (N-2)\times A_{min}$
$\therefore A_{min} + A_{max} \geq H$
Note that $A_{min} + A_{max} = H, H + (N-2)\times (H-1) \geq H\times (N-1)$ is only true when $N=2$.
We can greedily pick all the blocks from $A_{min}$ and $H-A_{min}$ from $A_{max}$ until $N=2$, where we can construct the last building.
This can be solved naively with $O(N^2)$, but can solved in $O(NlogN)$ with data structures such as: std::multimap / std::multiset / std::priority_queue