| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3611 |
| 4 | jiangly | 3583 |
| 5 | strapple | 3515 |
| 6 | tourist | 3470 |
| 7 | Radewoosh | 3415 |
| 8 | Um_nik | 3376 |
| 9 | maroonrk | 3361 |
| 10 | XVIII | 3345 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 162 |
| 2 | adamant | 148 |
| 3 | Um_nik | 145 |
| 4 | Dominater069 | 143 |
| 5 | errorgorn | 141 |
| 6 | cry | 138 |
| 7 | Proof_by_QED | 136 |
| 8 | YuukiS | 135 |
| 9 | chromate00 | 134 |
| 10 | soullless | 132 |
| Название |
|---|



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.