How to use binary search in this question?
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3839 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3612 |
7 | Geothermal | 3569 |
8 | ecnerwala | 3494 |
9 | Um_nik | 3396 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | Um_nik | 164 |
2 | -is-this-fft- | 162 |
3 | maomao90 | 159 |
3 | atcoder_official | 159 |
5 | cry | 158 |
5 | awoo | 158 |
7 | adamant | 155 |
8 | nor | 154 |
9 | TheScrasse | 153 |
10 | Dominater069 | 152 |
How to use binary search in this question?
Name |
---|
Find some $$$f(h)$$$ that gives the number of cards required for a given height $$$h$$$, prove that it's monotonic (that is, if $$$h_1 < h_2$$$, then $$$f(h_1) \leq f(h_2)$$$), and binary search on the lowest $$$h_0$$$ such that $$$f(h_0)$$$ is less than or equal to the cards that you have, then repeat while you can still make towers.
thanks
galen_colin Could you please help me in deriving time complexity for given $$${{N}}$$$
Does it converge to $$${{O(\log(\sqrt N))}}$$$ in the binary search approach ?
Let's see. $$$f(h + 1) - f(h) = \frac{(h + 1)(3h + 4)}{2} - \frac{h(3h + 1)}{2} = \frac{3h^2 + 7h + 4}{2} - \frac{3h^2 + h}{2} = 4h + 1$$$, which is an upper bound on the remaining number of cards. Since $$$h \approx \sqrt{n}$$$, each iteration the number of cards will become around a square root of itself. So you get $$$\log{n^{\frac{1}{2}}} + \log{n^{\frac{1}{4}}} + \dots = \frac{\log{n}}{2} + \frac{\log{n}}{4} + \dots... = \log{n}$$$, approximately. Maybe the factor of $$$4$$$ plays a role, but probably not too much of one.
Thanks. So the Time complexity is: $$${{O(t\cdot\log(N))}}$$$
This reminds me the first problem of Codejam'20 Round 2.