From a hidden array $A$ of length $N$, Stack constructs an array $B$ of length $N$ such that:
- For all $i$ $(1 \leq i \leq N)$, $B_i=\max(A_1,A_2,\ldots,A_i)$ or $B_i=\min(A_1,A_2,\ldots,A_i)$.
For the given array $B$ of length $N$, Stack wants you to check whether a possible array $A$ exists or not.
Input Format
- The first line of input contains a single integer $T$, denoting the number of test cases. The description of $T$ test cases follow.
- The first line of each test case contains an integer $N$ - the length of the array $B$.
- The second line of each test case contains $N$ space-separated integers $B_1,B_2,\ldots,B_N$ representing the array $B$.
Output Format
For each test case, output in a single line YES
if $A$ exists, otherwise print NO
.
You may print each character of the string in uppercase or lowercase (for example, the strings YES
, yEs
, yes
, and yeS
will all be treated as identical).
Constraints
- $1 \leq T \leq 10^5$
- $1 \leq N \leq 10^5$
- $1 \leq B_i \leq 10^5$
- Sum of $N$ over all test cases does not exceed $10^5$.
Sample Input 1
3
1
343
4
1 1 2 3
3
1 3 2
Sample Output 1
YES
YES
NO
Explanation
Test case $1$: The only possible array is $A=[343]$.
Test case $2$: One possible array is $A=[1,1,2,3]$. In this case $B_i = max(A_1, A_2, \ldots, A_i)$ satisfies for all $1 \le i \le 4$.
Test case $3$: It can be proven that there exists no $A$ from which we can obtain the given array $B$.
Sample Input 2
2
5
1 1 1 1 1
5
1 2 1 2 1
Sample Output 2
YES
YES
Explanation
Test case $1$: The only possible array is $A=[1,1,1,1,1]$.
Test case $2$: One possible array is $A=[1,2,1,1,1]$. In this case,
- $B_1=A_1$
- $B_2=\max(A_1,A_2)$
- $B_3=\min(A_1,A_2,A_3)$
- $B_4=\max(A_1,A_2,A_3,A_4)$
- $B_5=\min(A_1,A_2,A_3,A_4,A_5)$
No comments:
Post a Comment