Omkar and Medians
Contest: Codeforces Round 724 (Div. 2)
Difficulty rating: 2000
Tags: data structures, greedy, implementation

Uh oh! Ray lost his array yet again! However, Omkar might be able to help because he thinks he has found the OmkArray of Ray's array. The OmkArray of an array $$$a$$$ with elements $$$a_1, a_2, \ldots, a_{2k-1}$$$, is the array $$$b$$$ with elements $$$b_1, b_2, \ldots, b_{k}$$$ such that $$$b_i$$$ is equal to the median of $$$a_1, a_2, \ldots, a_{2i-1}$$$ for all $$$i$$$. Omkar has found an array $$$b$$$ of size $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$, $$$-10^9 \leq b_i \leq 10^9$$$). Given this array $$$b$$$, Ray wants to test Omkar's claim and see if $$$b$$$ actually is an OmkArray of some array $$$a$$$. Can you help Ray?

The median of a set of numbers $$$a_1, a_2, \ldots, a_{2i-1}$$$ is the number $$$c_{i}$$$ where $$$c_{1}, c_{2}, \ldots, c_{2i-1}$$$ represents $$$a_1, a_2, \ldots, a_{2i-1}$$$ sorted in nondecreasing order.

Input format:
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Description of the test cases follows.

The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the array $$$b$$$.

The second line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$-10^9 \leq b_i \leq 10^9$$$) — the elements of $$$b$$$.

It is guaranteed the sum of $$$n$$$ across all test cases does not exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case, output one line containing YES if there exists an array $$$a$$$ such that $$$b_i$$$ is the median of $$$a_1, a_2, \dots, a_{2i-1}$$$ for all $$$i$$$, and NO otherwise. The case of letters in YES and NO do not matter (so yEs and No will also be accepted).

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the second case of the first sample, the array $$$[4]$$$ will generate an OmkArray of $$$[4]$$$, as the median of the first element is $$$4$$$.

In the fourth case of the first sample, the array $$$[3, 2, 5]$$$ will generate an OmkArray of $$$[3, 3]$$$, as the median of $$$3$$$ is $$$3$$$ and the median of $$$2, 3, 5$$$ is $$$3$$$.

In the fifth case of the first sample, the array $$$[2, 1, 0, 3, 4, 4, 3]$$$ will generate an OmkArray of $$$[2, 1, 2, 3]$$$ as

- the median of $$$2$$$ is $$$2$$$
- the median of $$$0, 1, 2$$$ is $$$1$$$
- the median of $$$0, 1, 2, 3, 4$$$ is $$$2$$$
- and the median of $$$0, 1, 2, 3, 3, 4, 4$$$ is $$$3$$$.

In the second case of the second sample, the array $$$[1, 0, 4, 3, 5, -2, -2, -2, -4, -3, -4, -1, 5]$$$ will generate an OmkArray of $$$[1, 1, 3, 1, 0, -2, -1]$$$, as

- the median of $$$1$$$ is $$$1$$$
- the median of $$$0, 1, 4$$$ is $$$1$$$
- the median of $$$0, 1, 3, 4, 5$$$ is $$$3$$$
- the median of $$$-2, -2, 0, 1, 3, 4, 5$$$ is $$$1$$$
- the median of $$$-4, -2, -2, -2, 0, 1, 3, 4, 5$$$ is $$$0$$$
- the median of $$$-4, -4, -3, -2, -2, -2, 0, 1, 3, 4, 5$$$ is $$$-2$$$
- and the median of $$$-4, -4, -3, -2, -2, -2, -1, 0, 1, 3, 4, 5, 5$$$ is $$$-1$$$

For all cases where the answer is NO, it can be proven that it is impossible to find an array $$$a$$$ such that $$$b$$$ is the OmkArray of $$$a$$$.

Example input:
5
4
6 2 1 3
1
4
5
4 -8 5 6 -7
2
3 3
4
2 1 2 3

Example output:
NO
YES
NO
YES
YES

Example input:
5
8
-8 2 -6 -5 -4 3 3 2
7
1 1 3 1 0 -2 -1
7
6 12 8 6 2 6 10
6
5 1 2 3 6 7
5
1 3 4 3 0

Example output:
NO
YES
NO
NO
NO

Editorial:
For some $$$k<n$$$, assume $$$b_1, b_2, \cdots, b_{k}$$$ is the OmkArray of some $$$a_1, a_2, \cdots, a_{2k-1}$$$, and we want to see what values of $$$a_{2k}, a_{2k+1}$$$ we can add so that $$$b_1, b_2, \cdots, b_{k+1}$$$ is the OmkArray of $$$a_1, a_2, \cdots, a_{2k+1}$$$. Let $$$c_1, c_2, \cdots, c_{2k-1}$$$ be $$$a_1, a_2, \cdots, a_{2k-1}$$$ sorted in ascending order.
If $$$b_{k+1} \geq b_k$$$, note that $$$b_{k} = c_k$$$ and there are $$$k-2$$$ elements of $$$a$$$ $$$\geq c_{k+1}$$$, so no matter how large $$$a_{2k}, a_{2k+1}$$$ are there will be at most $$$k$$$ elements larger than $$$c_{k+1}$$$ in $$$a_1, a_2, \cdots, a_{2k+1}$$$. This gives $$$b_{k+1} \leq c_{k+1}$$$. We can use a similar argument to show $$$b_{k+1} \geq c_{k-1}$$$. Now we want to bound $$$c_{k+1}$$$ and $$$c_{k-1}$$$. Note that each distinct value among $$$b_1, b_2, \cdots, b_k$$$ must appear at least once in $$$a_1, a_2, \cdots, a_{2k-1}$$$. Therefore, if $$$i$$$, $$$j$$$ satisfy that $$$b_i$$$ is the largest value of $$$b_i\leq b_k$$$ and $$$i \neq k$$$, and $$$b_j$$$ is the smallest value of $$$b_j \geq b_k$$$, $$$j \neq k$$$, then we have $$$c_{k+1} \leq b_j$$$, $$$c_{k-1} \geq b_i$$$, and so $$$b_i \leq b_{k+1} \leq b_j$$$. If no such largest/smallest values exist, then we can assume $$$b_{k+1}$$$ is not bounded above/below.
Therefore, if $$$b$$$ has an OmkArray, it is necessary that for all $$$i$$$, there does not exist a $$$j\leq i$$$ such that $$$b_j$$$ is between $$$b_i$$$ and $$$b_{i+1}$$$, exclusive. I claim this is also sufficient. We can construct such an array $$$a$$$ using the following algorithm:
- Let $$$a_1 = b_1$$$.
- If $$$b_{i+1}=b_j$$$ for some $$$b_j<b_i$$$ with $$$j<i$$$, let $$$a_{2k-2}, a_{2k-1} = -\infty$$$ (we can replace $$$-\infty$$$ with some sufficiently small constant at the end of our array creation process).
- Otherwise, if $$$b_{i+1}<b_i$$$ then let $$$a_{2k-2} = -\infty$$$, $$$a_{2k-1} = b_{i+1}$$$.
- If $$$b_{i+1}=b_j$$$ for some $$$b_j>b_i$$$ with $$$j<i$$$, let $$$a_{2k-2}, a_{2k-1} = \infty$$$ (we can replace $$$\infty$$$ with some sufficiently large constant at the end of our array creation process).
- Otherwise, if $$$b_{i+1}>b_i$$$ then let $$$a_{2k-2} = \infty$$$, $$$a_{2k-1} = b_{i+1}$$$.
- Finally, if $$$b_{i+1} = b_i$$$, let $$$a_{2k-2} = -\infty$$$, $$$a_{2k-1} = \infty$$$.
This means that an equivalent condition to having an OmkArray is for all $$$i$$$, there does not exist a $$$j\leq i$$$ such that $$$b_j$$$ is between $$$b_i$$$ and $$$b_{i+1}$$$, exclusive. There are multiple ways to check this for an array $$$b$$$, but one clean way would be to keep some TreeSet $$$s$$$, and check if $$$b_{i+1}$$$ is between s.ceil($$$b_i$$$) and s.floor($$$b_i$$$) for all $$$i$$$, and then adding $$$b_{i+1}$$$ to $$$s$$$ if it is not already added.