How to use binary search in this question?
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
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.