This is the editorial for the Unofficial Div 4 Round #2 created by SlavicG and mesanu.
We hope everyone had fun and enjoyed the contest!
Problem A — Catching the Impostor.
TutorialThe problem is just implementation. Check if exactly $$$n-1$$$ numbers are encountered. You can easily do this using a set or a frequency array.
Problem B — Epic Permutation
Tutorial There is only one epic permutation for each $$$n$$$, and the permutation always looks like this [ $$$1$$$, $$$2$$$, $$$3$$$,... ]. This is because the greatest gcd we can obtain for each position $$$i$$$ and the element at that position is the position itself if we choose the element to be equal to the position. So the answer is just the sum of the first $$$n$$$ positive integers. It can be calculated using a loop because of the small constraints or using the formula $$$\frac{n \cdot (n+1)}{2}$$$.
Problem C — Similar Arrays
Tutorial It can be proven that the best number to create the array with, is the median (the middle element when the array is sorted) of the initial array. So, sort the array. Now, the median is the middle element $$$(med = a[n/2])$$$. To get the answer iterate over the array and add $$$abs(a_i - med)$$$ to it.
Proof why the median is the best choice We denote the median as $$$m$$$. We have the sorted array $$$a$$$ where some elements are smaller, some are equal, and some are larger than $$$m$$$ in this order. Let's consider the other possible choices which are, choosing a smaller value than $$$m$$$ and a larger value than $$$m$$$ if we choose a greater value than $$$m$$$, then the difference between the elements smaller and equal to $$$m$$$ will increase by $$$x - m$$$. And the difference between the elements greater than $$$m$$$ will increase by $$$x - m$$$. But, because there are at least as many elements equal or smaller to $$$m$$$ as elements greater than $$$m$$$ we can see it is not optimal to choose this number, because in the best case, the difference wouldn't change and in the worst case, it would become greater. The same technique can be used for numbers smaller than $$$m$$$. This leaving $$$m$$$ as the best choice.
Problem D — Sanda's Job
Tutorial We need to find if the difference between $$$n$$$ and $$$x$$$ is also a permutation of $$$x$$$'s digits. There are many ways to do this, and our solution is to transform $$$x$$$ and $$$n-x$$$ into strings, then sort the strings and see if they are equal. This is possible in C++ using to_string(int value) function.
Problem E — Count Substrings
Tutorial We iterate through string $$$s$$$, and whenever we encounter a substring $$$t$$$, we add to the answer the number of unique substrings we can make using it. We can calculate this with the formula (number of characters since the start of the last encounter)$$$\cdot$$$(the number of characters until the end of the string). This is because the start of the substring must be between the start of the last $$$t$$$ substring in $$$s$$$ and the start of the $$$t$$$ substring just encountered(so we don't double count previous substrings). The end must be between the last element of this $$$t$$$ substring and the last element of string $$$s$$$.
Problem F — Game on Grid
Tutorial Suppose that squares with odd column and odd row are coloured red, odd column and even row are coloured green, even column and odd row are coloured blue, even column and even row are coloured gray.
If n = 0 (mod2) then you can split the grid into sections of squares with 4 smaller squares each, wherever Bob colours he will at most nullify 4 squares. If n = 2 (Mod 4) then if Bob always plays correct(nullifying 4 squares every moves) Alice still wins because they will play for an odd number of moves.
If n = 0 (mod 4) then then Bob's strategy is to just eliminate one colour. Because Alice needs all colours present to move the game will end in $$$\frac{n^2}{8}$$$ moves, this number being even so because playing optimally they can at most get 4 squares each move each will get the same number of squares.
If n is odd, then Bob will focus on the colour that is least present ( even column, even row ). Alice will get $$$4\cdot(\lfloor\frac{(n-1)^2}{8}\rfloor+1)$$$ and Bob will get $$$4\cdot\lfloor\frac{(n-1)^2}{8}\rfloor + 2\cdot n-1$$$ so Bob will get more for $$$n \geq 3$$$.
Exception for $$$n = 1$$$ but its easy to see that Bob would win.