Can Someone please explain the solution of the problem Link
I know its binary search problem but how to implement the check function
# | User | Rating |
---|---|---|
1 | tourist | 3690 |
2 | jiangly | 3647 |
3 | Benq | 3581 |
4 | orzdevinwang | 3570 |
5 | Geothermal | 3569 |
5 | cnnfls_csy | 3569 |
7 | Radewoosh | 3509 |
8 | ecnerwala | 3486 |
9 | jqdai0815 | 3474 |
10 | gyh20 | 3447 |
# | User | Contrib. |
---|---|---|
1 | maomao90 | 174 |
2 | awoo | 165 |
3 | adamant | 162 |
4 | TheScrasse | 160 |
5 | nor | 158 |
6 | maroonrk | 156 |
7 | -is-this-fft- | 152 |
8 | orz | 146 |
9 | SecondThread | 145 |
9 | pajenegod | 145 |
Can Someone please explain the solution of the problem Link
I know its binary search problem but how to implement the check function
Name |
---|
Since the problem asks to find the lowest possible median, we can binary search a possible value mid and check if the lowest median can be less than or equal to mid.
To check if any of the medians are $$$\leq \text{mid}$$$, consider each $$$K \times K$$$ submatrix separately. How to check if the median here is $$$\leq \text{mid}$$$? Let $$$\text{numMore}$$$ be the number of values in the submatrix that are strictly greater than $$$\text{mid}$$$. Since the median is defined as the $$$\frac{K^2}{2} + 1$$$ th highest element, we must have that $$$\text{numMore}$$$ be less than that value. It's not too hard to see why this holds; if $$$\text{numMore}$$$ is less than that value, then there will be a value $$$\leq \text{mid}$$$ in the $$$\frac{K ^ 2}{2} + 1$$$ th highest position, and if $$$\text{numMore}$$$ is more than that value, then the median will be greater than mid.
So we go through all the $$$K \times K$$$ submatrices and check whether any of the medians are $$$\leq \text{mid}$$$, and update our binary search values accordingly.
To speed up the process of finding the number of values greater than $$$\text{mid}$$$ in a submatrix, we can use prefix sums. Final complexity turns out to be $$$O(N^{2}\text{log}MAX(A_i))$$$
My code with some comments is linked here: https://atcoder.jp/contests/abc203/submissions/23086034
Thanks for helping...!!