| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | Benq | 3676 |
| 2 | Kevin114514 | 3659 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3382 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3312 |
| 10 | idontwannadothis…e | 3191 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 156 |
| 2 | nik_exists | 151 |
| 3 | maspy | 149 |
| 4 | AmShZ | 145 |
| 5 | Um_nik | 143 |
| 6 | Errichto | 139 |
| 7 | BledDest | 136 |
| 8 | adamant | 135 |
| 9 | maroonrk | 132 |
| 10 | cry | 130 |
| Название |
|---|



Yes, the problem is very difficult. At least at my level. Looking at bruteforce I can see that there needs to be some property that will reduce the runtime from $$$O(N^3)$$$ to at least $$$O(N ({\log N})^k)$$$.
When you think about the final array, you know some useful numbers. $$$\frac{N(N+1)}{2}$$$ is the number of elements in it. Similarly, when number
xis a median of that array, there must be at least $$$\frac{1}{2} \cdot \frac{N(N+1)}{2}$$$ elements>=x. For example, when there's only 1 unique number in that array, this property holds, when all numbers are unique, this property holds.So if you can somehow count the number of elements
>=xin that array of subarray medians quickly (e.g. $$$O(N)$$$ or $$$O(N \log N)$$$) you can binary search for the moment when this count drops below $$$\frac{1}{2} \cdot \frac{N(N+1)}{2}$$$, all numbers greater than median will have less than half numbers greater than or equal to them.What you notice first is that you do not have that array with $$$\frac{N(N+1)}{2}$$$ elements. You only have $$$N$$$ elements, but, the condition for counting still holds. If you can count the number of subarrays where there were more than $$$\frac{L}{2}$$$ (
Lis length of subarray) elements>=x, you also count all of the elements in that array of subarray medians that are>=x. The way you count this is by +1 -1 mapping and count all subarrays where sum of that is>=0. In that case you know that in each subarray there was $$$\geq\frac{L}{2}$$$ elements greater thanx. When sum is 0, the element was the median as there was equal number of<xand>=x, when sum is>0the element might have been median but it also might be less than median, if<0thenxis surely higher than median because more than half of array was smaller.Now, you have binary search on
xand this counting subproblem. This counting subproblem with>=0is somewhat related to a simpler problem of "counting all the sums in subarrays where sum is equal to K". This one we know how to do in linear time. The difference here is that>=Kis a bit more involved, because every time your prefix sum appears, you need to include not just the count ofprefixSum-Kbut all previous sums whereprefixSum-K>=0. You can do this with a BIT, counting all sums on the range-Nandp+1(exclusive, wherepis the current prefix sum). Or I assume they use "inversion number" for whatever they use to count this. I do not know what that is.Below is $$$O(N^3)$$$ that calculates this count for each element in
abut you want to binary search it and get this sum efficiently.With BIT, this is a $$$O(N (\log N)^2)$$$ solution. There is a way to not use BIT and get $$$O(N \log N)$$$, this way exploits the fact that your prefix sum
ponly increases/decreases by 1. I'll skip that here.