### Geothermal's blog

By Geothermal, history, 22 months ago,

Hi all!

As it might be a little while before the editorial for today's round is released (due to the open hacking phase), I thought I'd publish my solutions in case anyone wants to review the problems right away.

# A — Divisibility Problem

There are a few ways to approach this. One is to find the next multiple of $B$ after $A$ and subtract $A$. Recall that we can compute $\frac{A}{B}$, rounded up, as $\frac{A+B-1}{B}$, rounded down. We can compute this value and then multiply it by $B$ to get the next multiple of $B$, subtracting $A$ to get our answer.

An alternative solution (and the one I implemented) is to realize that, as long as $A$ isn't already a multiple of $B$, the number of moves required is $B - (A \% B)$, since the distance to the next lower multiple of $B$ plus the distance to the next higher multiple of $B$ will be $B$. We can use this to compute the answer. In the case where $A$ is a multiple of $B$, this prints $B$ instead of $0$, so we can take this value modulo $B$ to compute the correct answer.

Runtime: $O(1)$. Click here for my submission.

# B — K'th Beautiful String

We build the string character-by-character. At any stage, we want to find the $K$'th lexicographically smallest string starting with a certain prefix and including $X$ more characters, including $Y$ b's. Then, there are $Z = \dbinom{X-1}{Y}$ strings satisfying these conditions and with an 'a' as the next character. These strings are lexicographically smaller than all strings with b's as the next character, so if $K \leq Z$, we'll use one of these strings, and we should append an 'a' and continue with $X$ decreased by $1$. Otherwise, we need to append a 'b', so for our next stage, we want to find the $(K-Z)$'th lexicographically minimal string with $X-1$ more characters and $Y-1$ more b's.

Runtime: $O(N)$. Click here for my submission.

# C — Ternary XOR

We build the answer digit by digit. Notice that the two numbers will be equal up to a certain point. As soon as we make the two numbers different, though, whichever number is bigger at this stage will always be bigger, no matter what we do, so we should seek to minimize this number without caring what happens to the smaller number. This means that as soon as we add different characters to the two numbers, every remaining digit in the larger number should be a zero, while the remaining digits in the smaller number should be the same as the digits of the input number.

Until we reach this point, though, we should try to keep the two numbers as balanced as possible to avoid making the larger one too big. When we process a $0$, we are clearly best off adding a $0$ to each number. When we process a $2$, we should add a $1$ to each number: this is better than adding a $0$ to one number and a $2$ to the other because this gives a maximum of $2$ rather than $1$. When we process a $1$, we must add a $1$ to one number and a $0$ to the other, and from here, we move into the stage described above, adding only $0$'s to the number that received the $1$ and appending all digits from the input to the number which received a $0$.

Runtime: $O(N)$. Click here for my submission.

# D — Carousel

First, note that the answer will always be at most $3$. To achieve this, alternate between $1$ and $2$ for the first $N-1$ numbers, with the last number receiving a $3$. We can easily see that this guarantees that all adjacent figures have distinct colors, so we will never have a pair of adjacent figures with the same color and distinct types. (The reason $2$ may not be achievable is because the carousel is circular, so the last figure is adjacent to the first--this makes the odd case harder to deal with, since, for example, when $N=3$, we could end up with a result of 1, 2, 1, which doesn't work if the first and last figures are of different types.)

Now, we determine whether we can do better. It is trivial to check if the answer is $1$: this works only if all figures are of the same type. Now, we simply need to check if the answer is $2$ and provide a construction if so. To do this, we use dynamic programming. Without loss of generality, let the first figure have color $1$. Then, if it is possible to color the first $i$ figures in a valid way such that figure $i$ has color $j$, let $dp[i][j]$ be the color of figure $i-1$ used in one such coloring. Otherwise, let $dp[i][j] = -1$. To transition, if $dp[i][j] \neq -1$, we can transition to $dp[i+1][3-j]$ (if we let the colors be $1$ and $2$) from $dp[i][j]$, and if figures $i$ and $i+1$ have the same color, we can also transition to $dp[i+1][j].$

From here, we can determine whether an answer of $2$ is possible. If $dp[N-1][2]$ isn't $-1$, then we can definitely get an answer of $2$. If $dp[N-1][1]$ isn't $-1$ and figure $N-1$ has a different color from figure $0$, we can also achieve an answer of $2$. Either way, we can pick one of these cases and backtrack through our DP table to compute a valid coloring, if one exists.

If no such coloring exists, the answer must be $3$, and we can output the construction described above.

Runtime: $O(N)$. Click here for my submission.

# E — Tree Queries

Let's start with an observation. Realize that if a path from the root goes through any vertex within distance $1$ of a node, it will also go through the vertex's parent. Thus, the problem is reduced to determining whether some path from the root contains the parents of all vertices in the query set. For simplicity, we simply replace every vertex in the query set with its parent (replacing the root with itself).

Since a path from the root to a vertex includes that vertex and its ascendants, we see that the answer is yes if and only if there is some node in the set that's a descendant of every other node in the set. We can perform a pre-order traversal, tracking entry and exit times, to enable us to determine in $O(1)$ whether some node $v$ is an ancestor of some node $u$. Then, we maintain a node that's a descendant of all nodes we've considered thus far. We process each node in the query set---if this node is an ancestor of our current node, we do nothing, and if the current node is an ancestor of this node, this node is now a descendant of all nodes we've considered, so we replace the current node with the new one. If neither node is an ancestor of the other, there is no vertex in the set that is a descendant of all the others, so the answer is no. If we finish this process and still have a valid result node, the answer is yes.

# F — Make k Equal

Suppose we want to make $K$ elements of the set equal to some number $X$. If there are at least $K$ copies of $X$ in the set, this takes $0$ operations. Otherwise, realize that to make any number lower than $X$ equal to $X$, we must first raise all numbers lower than $X$ to $X-1$. Meanwhile, to make any number higher than $X$ equal to $X$, we must first lower all numbers greater than $X$ to $X+1$. Take for granted that we can compute the costs of doing these things---we'll explain how to do this below. Then, if there are at least $K$ numbers less than or equal to $X$, we can bring all the lower numbers to $X-1$ and then raise however many of them we need to $X$. If there are at least $K$ numbers greater than or equal to $X$, we can bring all the higher numbers to $X+1$ and then lower however many we need to $X$. In any case, we can also bring all the lower numbers to $X-1$ and all the higher numbers to $X+1$, after which we can take however many numbers we need to $X$.

Now, how can we determine the number of operations this will take for each possible $X$ efficiently? First, a quick observation: we only need to consider elements of the input array as $X$, as we can prove that as long as $X$ is not an element of the array, we can increase or decrease it to the next element of the input array without increasing the number of required operations. Then, we consider each possible $X$ in increasing order. We can then maintain the number of elements less than $X$ and the sum of all those elements, as well as the same values for the numbers greater than $X$. These values allow us to compute the costs of bringing all numbers lower than $X$ to $X-1$ or the costs of bringing all numbers higher than $X$ to $X+1$, after which we can consider each of the three cases above to compute our answer.

Runtime: $O(N \log N)$, to account for sorting. Click here for my submission.

• +97

 » 22 months ago, # |   0 for problem D, my solution was thislet the continuous elements form a grp,if #grps == 1 then all same colorelse if #grps is even then only 2 colors (alternate colors) else if #grps is odd and #grps == n then 3 colorselse if #grps is odd and #grps < n then we can do with 2 colors only , we just have to color the last element of a continuous grp (of sz > 1) with a color diff from that of the members of the same grp and continue alternatingbut this gave WA on TC2, dont know why
•  » » 22 months ago, # ^ | ← Rev. 5 →   +9 Consider:n = 41 3 1 2 grps = 3grps < n (which is 4)The number of colors needed is 3. But according to you this is the case of Odd number of Groups Less than N which can be done in 2.The assumption that if #grps < n and if #grps is odd there must be a continuous group (of size > 1) is false. It is little hard to understand your explanation but this is the problem I see based on my interpretation.
•  » » » 22 months ago, # ^ |   0 In this case, it looks like #grps would be 4, not 3 because there aren't any adjacent spots with the same animal. Regardless, the number of colors needed would be 2 because you can just alternate between 1 and 2 at even and odd indices.
 » 22 months ago, # |   +9 You are doing amazing job Geo for writing detailed editorials . Keep it up
 » 22 months ago, # |   0 thanks a lot :)your editorial is actually readable, unlike many editorials here.
 » 22 months ago, # |   +1 If dp[N−1][1] isn't −1 and figure N−1 has a different color from figure 0Is this correct or you meant does not have a different color from figure 0
•  » » 22 months ago, # ^ |   +6 This is a typo—-I meant that the two figures should be of the same type. Nice catch; I’ll edit the main post tomorrow.
•  » » » 22 months ago, # ^ |   0 Yea, Thanks
 » 22 months ago, # |   -29 why u complicate simple things. problem d is overkill by ur approach. i dont like ur editorials and english.
•  » » 22 months ago, # ^ |   0 Simple Solution of problem Dif n==even , do 1 2 1 2 .... alternatively,PS. Dont forget to check if the seqn containsall elements of same value or not, bcoz if it does then ans => 1 1 1 ....Now this we know that even sequence is easy to solve so in case of odd sequence length we try to find a way to make it even i.e if we have 1 2 1 2 3 then coloring 1 2 1 2 1 would fail but if we had a pair with same value lets say like this 1 2 1 2 2 3 4 (took 4 to make n odd) then we can make this even by using the pair 2 2 coloring would be 1 2 1 2 2 1 2 bigger example 1 2 1 2 2 2 2 2 3 --> coloring 1 2 1 2 2 1 2 1 2 we basically availed the pair , kind or removed one number from pair by giving same color and hence the sequence even length and we know that even length sequence ans is alternating 1 and 2Hence Solved :)
•  » » » 22 months ago, # ^ |   0 yes, that is how i solved it. i don't understand why getothermal writes so complicated things for div3 editorials. does he forget that he is wriring for div3 participants. if he can't make things easy then dont make complicate.
 » 22 months ago, # |   0 thanks and orz
 » 22 months ago, # | ← Rev. 2 →   0 In editorial for problem F "we can prove that as long as X is not an element of the array, we can increase or decrease it to the next element of the input array without increasing the number of required operations." Can someone please explain the proof.
 » 22 months ago, # |   0 I do not know much about entry and exit time (any recommendations !) but looking at your code i got about it a little. pos is entry time and out is obviously exit. so i guess if a node is descesnt of another - - it pos is greater than pos of ancestor. - it out time is less than out time of ancestor. - did not understood the following line from your code. - pos[nxt] < pos[cur] && pos[cur] < out[nxt] - in above why are you comparing the in time with outime again.