I come to a fun problem, and after I tried hard to solve it, I curiously to find better algorithm, but I cant.
The problem is:
There are $$$N$$$ buildings with $$$a_1, a_2, \dots, a_n$$$ metters height. These days are hard, the heavy raining weather still not ended yet. In day $$$d$$$, every building with height $$$h$$$ only have $$$x_d = \lfloor \frac{h}{d} \rfloor$$$ height left of good space to use, while others are sunk underwater. Every building having the same value $$$x_d$$$ on day $$$d$$$ will group together (including $$$x_d = 0$$$ which sunk completely underwater) in a way that no other building with same value $$$x_d$$$ in another group.
The question is:
Output $$$N$$$ lines, in each size $$$s$$$ from $$$1$$$ to $$$n$$$, what is the earliest day $$$d$$$ that have at least one group of size $$$s$$$ (if there is no suitable day then output -1)
The constraints are:
- Subtaks 1: $$$n \leq 100~ and ~a_i \leq 2.10^5$$$
- Subtaks 2: $$$n \leq 300~ and ~a_i \leq 3.10^6$$$
- Subtaks 3: $$$n \leq 300~ and ~a_i \leq 5.10^7$$$
The examples are:
My approach to this problem:
My question
Is there a better algorithm for larger $$$N$$$ ? (upto $$$10^4, 10^6$$$)
Is there a better algorithm for larger $$$a_i$$$ ? (upto $$$10^{12}, 10^{16}, 10^{18}$$$)
Can I use combinatorics or euclidian algorithm for this problem ?








This is proof that ratism is real !
Always provide a source. It's so scary nowadays to answer any blog, especially during Codechef Long. People keep asking about problems from ongoing contests -_-
Ad here's maybe not that common opinion: beginners only waste time by modifying problems or trying to solve them for big constraints. There are thousands of problems out there that are for sure solvable.
:< I kept your advice from the last time but sometimes I found some interesting problems yet learning something new when solved it with a higher level & variant of the problems.
Yes sir but it is not possible to do so in this case that I am unable to give the source since it is from a private ongoing training contest whose class is over yet and my teacher move to somewhere out to teach but there is still the problem. And I am asking for permission to public the problem
Here is the source. The author allowed me to clone to a new problem and also wondering whether there is a better solution too ^^. I have also write both Vietnamese/English version of statements
Auto comment: topic has been updated by SPyofgame (previous revision, new revision, compare).
With value compression and lazy propagation for the next Harmony Number without creating $$$N$$$ vectors of such values for each $$$A_i$$$ individually. The problem can be solved in pure $$$O(n \sqrt{max(a_i)} + max(a_i))$$$ without any log. With my best of trying I managed to solve the problem with $$$1 \leq n \leq 1000$$$ and $$$1 \leq a_i \leq 10^8$$$ under 1 second (under
900±69ms). I did some literature research of relevant problems, there are NO better way of solving problem without having the $$$O(\sqrt{max(a_i)})$$$ related, but still possible to have a better complexity than $$$O(n \times \sqrt{max(a_i)})$$$ with tradeoff of $$$O(f(n))$$$ (currently $$$f(n)$$$ seems to be so big that not even worth trying). I did in fact have a blog about this years ago, but not yet to public it.You may found these useful in relevant harmony problems, I designed this on my own and use it in many floor & harmony related problems.
P/S: I did mention "but still possible to have a better complexity than" above, because:
In a relevant blog where I find the complexity $$$O(\sqrt[3]{g(a_i)})$$$ instead of $$$O(\sqrt[2]{g(a_i)})$$$, making the complexity from $$$O(T^{2/3}) \to O(T^{5/9})$$$. However, it requires many constraints to use, and this problem also have arbitrary $$$a_i$$$ instead of any related formula. Therefore such complexity $$$O(\sqrt[3]{g(a_i)})$$$ can not be achived with such conditions of $$$a_i$$$.
There are related works about other formulas better than $$$O(\sqrt[2]{g(a_i)})$$$. I tried most of them and find they are either wrong in the complexity calculation, or the constants were too high for the efforts. For certain constraints, we do in fact have another complexity than $$$O(n \times \sqrt{max(a_i)})$$$. For example, if the problem only ask for finding one abirtrary size, rather than all $$$n$$$ sizes.
I revisited this problem many times, and have a literature research for 3000 related articles, blogs, projects, ... and here are the answers for those questions from 5 years ago:
Is there a better algorithm for larger $$$N$$$ (upto $$$10^4$$$) ? —
YES, under 500ms (ai ≤ 3e7), under 900ms (ai ≤ 1e8)Is there a better algorithm for larger $$$N$$$ (upto $$$10^6$$$) ? —
NO, it will be MLEIs there a better algorithm for larger $$$a_i$$$ (upto $$$10^8$$$) ? —
YES, under 400ms (n ≤ 300), under 900ms (n ≤ 1e4)Is there a better algorithm for larger $$$a_i$$$ (upto $$$10^{10}$$$) ? —
MAYBE, if you can get rid of the O(max(ai)) factorIs there a better algorithm for larger $$$a_i$$$ (upto $$$10^{12}$$$) ? —
UNLIKELY, unless the problem only ask for a single arbitrary sizeIs there a better algorithm for larger $$$a_i$$$ (upto $$$10^{14}$$$) ? —
UNLIKELY, unless algorithms involve O(cbrt(g(ai)) existIs there a better algorithm for larger $$$a_i$$$ (upto $$$10^{16}$$$) ? —
UNLIKELY, having no ideas or hope to solve under 1 secondCan I use combinatorics algorithms for this problem ? —
UNLIKELY, seems unrelatedCan I use euclidean algorithms for this problem ? —
UNLIKELY, seems unrelated