Why do we take sqrt(N) as block size in algorithm suppose i take 5 as block size what would be the difference?
№ | Пользователь | Рейтинг |
---|---|---|
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 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
Why do we take sqrt(N) as block size in algorithm suppose i take 5 as block size what would be the difference?
Название |
---|
We are given queries(expressed as [l,r] interval, 1<=l<=r<=n) and sort them with group number that l is in, if same sort with r. suppose number of groups is g and block size is b. (g * b == q)
We will answer query by moving left and right pointer, and we will count number of moves of each pointer. left pointer will move O(q * b) times.(worst when left pointer iterate whole block for each query) right pointer will move O(g * n) times. (worst when right cursor iterate whole sequence for each block that left pointer is at. Right pointer is only increasing when left pointer is in same block.)
we choose b = sqrt(n) so time complexity will be O((n+q) sqrt(n))
g * b == q if we maintain this we will be getting appropriate time complexity right?
Actually g * b = n, since you turn the complete array into g groups of size b. (DongwonShin wrote
q
by accident.) And that's just a fact. Maintaining this will not give you the optimal complexity, but just making sure that the algorithm works at all.The most important thing is that by sorting the queries in the special way, the complexity will be O(q·b + g·n) (see DongwonShin's explanation).
So the important question is, what is the optimal block size such that the algorithm will run the fastest.
If you for instance pick 5 as block size, then you will have the complexity O(q·5 + n / 5·n) = O(n2), which will be pretty bad. Turns out that if q ≈ n, then the optimal choice is . This gives the total complexity .