How to use binary search in this question?
# | User | Rating |
---|---|---|
1 | jiangly | 3976 |
2 | tourist | 3815 |
3 | jqdai0815 | 3682 |
4 | ksun48 | 3614 |
5 | orzdevinwang | 3526 |
6 | ecnerwala | 3514 |
7 | Benq | 3482 |
8 | hos.lyric | 3382 |
9 | gamegame | 3374 |
10 | heuristica | 3357 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | -is-this-fft- | 162 |
3 | Um_nik | 161 |
3 | atcoder_official | 161 |
5 | djm03178 | 157 |
5 | Dominater069 | 157 |
7 | adamant | 154 |
8 | luogu_official | 152 |
9 | awoo | 151 |
10 | TheScrasse | 148 |
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.