Under construction

Still being built. Things may change

Sequence

Time limit1.5sMemory limit1024 MB

Summary
Given an array of length N (up to 5 x 10^5), find the largest count of a median value over all subarrays, where the medians are the middle one or two sorted elements.
Level

Hard9 of 10

Topics
Array, Divide and conquer, Prefix sum
Solved
No attempts yet

Problem

In the realm of APIO, a young and brilliant student named Alice found a sequence of length NN: A\[0]A\[0], A\[1]A\[1], ⋯\cdots, A\[N−1]A\[N - 1]. She wants to share some of her discoveries with you. First, here are some definitions.

  • Define W(l,r,x)W(l, r,x) as ∑_i=lrI\[A\[i]=x]\displaystyle\sum\_{i=l}^{r}{\mathrm{I}\[A\[i] = x]}, the number of occurrences of xx in A\[l]⋯A\[r]A\[l] \cdots A\[r].
  • Define the set of medians of a non-empty integer sequence B\[0]B\[0] B\[1]B\[1] ⋯\cdots B\[k−1]B\[k - 1] as S(B\[0],B\[1],⋯ ,B\[k−1])S(\\{B\[0], B\[1], \cdots ,B\[k - 1]\\}). Alice computes it in two steps.
    • First, sort B\[0],B\[1],⋯ ,B\[k−1]B\[0], B\[1], \cdots ,B\[k - 1] in ascending order to get C\[0],C\[1],⋯ ,C\[k−1]C\[0], C\[1], \cdots ,C\[k - 1].
    • Then S=C\[⌊k−12⌋],C\[⌈k−12⌉]S = \\{C\[\lfloor \frac{k-1}{2} \rfloor ], C\[\lceil \frac{k-1}{2} \rceil ] \\}.
    • For example, S(6,3,5,4,6,2,3)=4S(\\{6, 3, 5, 4, 6, 2, 3\\}) = \\{4\\}, S(4,2,3,1)=2,3S(\\{4, 2, 3, 1\\}) = \\{2, 3\\}, and S(5,4,2,4)=4S(\\{5, 4, 2, 4\\}) = \\{4\\}.

Alice wants the maximum of max⁡_x∈S(l,r)W(l,r,x)\displaystyle\max\_{x \in S(l,r)}{W(l, r,x)} over all 0≤l≤r≤N−10 \le l \le r \le N - 1, where S(l,r)S(l,r) is S(A\[l],⋯ ,A\[r])S(A\[l], \cdots ,A\[r]). She already has the answer and wants you to write a program that verifies it.

Constraints

  • 1≤N≤5×1051 \le N \le 5 \times 10^5
  • 1≤A\[i]≤N1 \le A\[i] \le N

Examples1

  1. Example 1

    Input
    1
    1
    
    Expected output
    1