Hello everyone!
I invite you to the contest where I authored all the problems. There are tasks that couldn't make it to the Official Codeforces Round. But they are still not that bad :)
I hope you enjoy the problems. Thanks for participating!
If you have any feedback or suggestions, please feel free to leave them in the comments. Your feedback will help improve my future contests.
UPD: My apologies! The author's idea for problem D was incorrect. I hope that the solution is now correct. All the submissions for this problem were rejudged, and three samples were added.
UPD2: Problem D has been hopefully fixed. Many thanks to Yam for detecting and correcting the problem.
It's interesting how awful problems like B, F and H are in the same set as D and G. Were they rejected for CF round? Why? And why did you decide to just post them instead of keeping them for some other contest?
Firstly I was preparing these problems for CF. However, I received a proposal to create a contest for Algotester, and some of these problems were used there (but not all). I then decided to also post them on CF to make them accessible beyond Ukraine.
Just curious, why do you think that B and H are awful?
Here are my opinions on the problems (I didn't read D because I probably won't be able to solve, or it would take too long):
I also dislike it a lot, the solution is very obvious immediately. After reading for 20 seconds, you just have to implement "longest path from each vertex in a tree" which is boring.
It's ok I guess. I don't think the solution is too interesting, but it seems fine for an easy problem and isn't tedious to implement.
I like it quite a bit (other than that it uses floats, and the fact that the output says "output the maximum possible mean" which I think is an accident).
The proof that you can indeed make all the elements exactly equal to the mean of the whole grid, by first flipping all rows, then columns, is nice imo.
Nice problem, although I feel like tests might be weak (?). My program passes pretty comfortably even tho it uses up to like 2*10^7 __gcd calls.
Thank you for the feedback!
I fixed the statement in problem B.
They are very boring and standard. I'm sorry if it's offensive. From the announcement I got the impression that the contest is constructed from problems that were rejected for the round, and most of the problems look like it. I would reject everything but D and G, and for B/F/H I wouldn't feel like I need to explain the reason. But G seems like a decent problem for div2 round (maybe even for div1), and D is actually very nice. So that seemed weird and like a waste of good problems, that's why I asked.
Actually, D seemed so out of place that I had a suspicion that the author's solution might be incorrect since I certainly wouldn't waste a problem with such a nice solution. But I might have missed some simpler solution, so it didn't seem fair to suggest this. Looks like I was correct though.
It's funny that when we were making a round, we thought independently of task F and tried to make it D2B)))
Just amazing SashaT9, Good Going Buddy!! Just contacted you when I was specialist & you were Expert & now we both are one step ahead!!! Lets hope for the best and very best of us both...
If possible provide tutorial as well. It will be helpful for people who are new to competitive programming like me.
I haven't done it yet. Maybe I'll work on the editorial soon)
enjoyed the contest, however felt like there was a big difficulty gap between DG and rest. Thanks anyways
problem F is reversed of an ABC problem, where author asked to maximized diameter.
problem F is also subtask 3 of IOI Dreaming excluding the fact the edges are weighted
How to solve G?
$$$gcd(a_i + j, a_j + i) = (i+j)$$$ means that
Thus we know that $$$a_i = i \cdot k + j \cdot (k-1)$$$ for some $$$k$$$. Let's itterate through all possible pairs of $$$(i,k)$$$, if $$$k > 1$$$ we already know $$$j$$$ so we can test if the found pair works indeed.
Now, if we save all pairs that work in a set, we can note that pairs that are never counted are the ones in which both $$$k=1$$$ and $$$y=1$$$, which means $$$a_i = i$$$ and $$$a_j = j$$$, so just count all pairs of fixed points.
Complexity is $$$O(vmax \cdot \sqrt{vmax} \cdot log)$$$, but magically, it runs in < 100ms ( probably testcases are not very strong ). You can view my code here.
Nice solution! I have another approach:
$$$\gcd(a_i+j, a_j+i) = i+j \leftrightarrow \gcd((a_i-i)+i+j, (a_j-j)+i+j) = i+j$$$
Now, if we assign a new array $$$b_i = a_i - i$$$, the equation becomes $$$\gcd(b_i + i + j, b_j + i + j) = i + j$$$
Here, we notice that $$$i+j$$$ may be a divisor of $$$b_i$$$, so we can iterate through all the divisors of $$$b_i$$$. If the divisor is $$$d$$$, we get $$$i+j=d \leftrightarrow j=d-i$$$, and all we need to do is check if such $$$j$$$ satisfies the equation. Complexity is $$$O(n\sqrt{maxA}\log n)$$$.
Really liked this problem personally, Thanks for sharing !!!
How to solve E?
Declare a new array $$$c$$$ of length $$$2 \cdot n$$$, where $$$c_{2i} = \max(a_i, b_i)$$$ and $$$c_{2i+1} = \min(a_i, b_i)$$$. After that, you can find LIS on this array and obtain the answer to the problem.
This algorithm works because in such constructions, we can't take both $$$a_i$$$ and $$$b_i$$$ simultaneously (since $$$\max(a_i, b_i) \geq \min(a_i, b_i)$$$).
Thank you :)
How to solve problem D. SashaT9 can you give me some hints/solution idea thanks in advance :)
Consider only the first and last occurrence of each letter.
To make all the letters equal, you will perform fewer than 26 operations. Try iterating through each starting letter.
Greedy doesn't work. Maybe generalize the idea with DP?
For each letter $$$c$$$, let's store its first and last occurrence: $$$l_c, r_c$$$. Now, if we want to perform an operation with letter $$$c$$$, the cost will be $$$r_c - l_c$$$. After this, $$$l_{c+1} = \min(l_{c+1}, l_c)$$$ and $$$r_{c+1} = \max(r_{c+1}, r_c)$$$.
Let $$$\text{meet}(l, r)$$$ be the letter we will encounter if we want to get from letter $$$l$$$ to letter $$$r$$$, and let $$$sz(l, r)$$$ be $$$r_{\text{meet}(l, r)} - l_{\text{meet}(l, r)}$$$ when we have already moved all letters up to $$$meet(l, r)$$$.
Now we can write a range DP.
Let $$$dp(l, r)$$$ be the minimal total cost to get from letter $$$l$$$ to letter $$$r$$$.
The transitions are:
At first, we move all the letters from $$$k+1$$$ to $$$r$$$. Then, we move all the letters from $$$l$$$ to $$$k$$$. Now, we need to get from $$$k$$$ to $$$r$$$. The size of the segment is $$$sz(l,k)$$$, and we will do exactly $$$\text{meet}(k+1, r) - \text{meet}(l, k)$$$ such operations.
The complexity is $$$O(A^4)$$$ where $$$A$$$ is the size of the alphabet. The DP works in $$$O(A^3)$$$ and we process it starting from each character.
The solution was proposed by Yam.
It won't really make any difference in practice since the domitaing part of the code is reading input, but we can optimize the dp to $$$O(A^3)$$$. Instead of calculating the dp separately for each starting point, we can calculate the dp once for a cyclic array.
My dp is similar to the one you explained, but possibly slightly different.
can somebody provide solution for problem A and B
Sure : So I have observed a pattern by writing bruteforce solution and running for 1-100
Can somebody explain how to solve problem C.
Let's find the subsequence with maximal length for which the greatest common divisor is equal to $$$d$$$. We can use numbers $$$d, 2d, \dots, sd$$$, and their GCD is guaranteed to be at least $$$d$$$.
Now, the observation is that if there exists a subsequence of length $$$k$$$ with GCD $$$d$$$, then there is also a subsequence with a length of $$$k-1$$$ and a GCD of $$$d$$$ (since we may choose not to include one of the elements from the set $$$d, 2d, \dots, sd$$$).
The algorithm is as follows:
Complexity is $$$O(n+A\log A)$$$.
I know the solution to problem A
If n + 2 is power of two prints n + 1 ,else print -1
Can anyone help me why this is the solution?
maybe you cant find it with condradiction
if x <= n, then after xor-ing every permutation, there is always 0 in the end
if x > n, then exist at the end of operation, exist 1 ⊕ x, which equal to x+1, if x is even, or x- 1, if x is odd. of course we cannot use x even, because it will produce x+1, which more than n, therefore, the array after operation wont be a permutation
so the only possible x is n+1.
Now, why the only possible solution for x is (2^n — 1, a.k.a 111...111₍₂₎). If x is not 2^n — 1, then x contain 0 in its biner representation, therefore, you can pick a number "i" that's reversal of x in its biner representation. i <= n < x
Example
x = 111110₍₂₎, then pick i = 000001₍₂₎, i ⊕ x = 111111₍₂₎, therefore i ⊕ x is bigger than n
So, the only solution is where x = n+1, and x = 111...111₍₂₎
How to solve problem F ?
let a = diameter of first tree, and b = diameter of second tree
ans = max(max(a, b), ceil(a/2) + ceil(b/2) + 1)
Any hint to solve F? My idea got WA. My idea is that we find node in both tree where the longest path starting from that node. After that, we can add an edge between both node and start doing dfs to find the diameter of the merged tree.
You should use dsu to solve this problem
Care to give me more hint about it? hehe
When you merge two trees it is enough to know diameter of each tree to find the minimum diameter
Sorry. I thought that there was queries. So you don't need dsu. You only need to know the diameters of each tree to find result
how do you manage to solve more than 5k problems within 3 years?
It was so exciting that I didn't even notice the number of tasks I solved:)