I notice there are some problems in binary search category where you have to find k-th element of a sequence like[Ntarsis' Set][K-th Not Divisible by n]. How can I develop this kind of problem solving approach.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
I notice there are some problems in binary search category where you have to find k-th element of a sequence like[Ntarsis' Set][K-th Not Divisible by n]. How can I develop this kind of problem solving approach.
Name |
---|
In problems where you need to find the k-th element in terms of a certain criterion (for example k-th smallest), you could store the numbers in a vector and sort them accordingly using a custom comparator. While this approach allows finding k_th element in O(log N), it does not support updates to the array (removal, insertion, modification ...). The problem with using std::set is iterator advancement has linear complexity. That is why a certain data structure called ordered_set is used. You can learn about them here:https://mirror.codeforces.com/blog/entry/11080. While this data structure may seem like a great alternative to set, this data structure should be used only when necessary as it has a huge constant factor and will result in TLE when used excessively. The problems you provided do not need this data structure, they can be handled with classic binary search with a check() function. If the check function tells you that the current element comes later than the k-th element (k+1th perhaps), you should adjust L or R accordingly. I hope this helps.