Skip to content

2171D. Rae Taylor and Trees (easy version)

binary search,data structures,dp,dsu,greedy,implementation,trees,*1400

https://codeforces.com/problemset/problem/2171/D

"To think a commoner would even fathom sitting next to me. Know your place!"

— Claire François

This is the easy version of the problem. The only difference between the easy and hard versions is that the hard version asks you to construct an example of a satisfactory tree.

As an Earth mage, Rae has mastered the spell of growing trees! But Manaria brags that she can grow a more impressive species of trees. Rae remembers that the most rare type of tree can be grown using a formula represented by a certain permutation — please help her construct it!

You are given a permutation∗ 𝑝 of length 𝑛.

Determine if there exists an undirected tree with 𝑛 vertices labeled 1,2,…,𝑛, satisfying the following condition:

  • Let 𝑢 and 𝑣 (1≤𝑢<𝑣≤𝑛) be any two vertices connected by an edge. Then 𝑢 appears before 𝑣 in 𝑝.

∗A permutation of length 𝑛 is an array that contains every integer from 1 to 𝑛 exactly once, in any order.

Input

The first line contains a single integer 𝑡 (1≤𝑡≤104) — the number of test cases.

The first line of each test case contains a single integer 𝑛 (2≤𝑛≤2⋅105).

The second line of each test case contains 𝑛 integers, 𝑝1,𝑝2,…,𝑝𝑛 (1≤𝑝𝑖≤𝑛). It is guaranteed that all 𝑝𝑖 are distinct.

It is guaranteed that the sum of 𝑛 over all test cases does not exceed 2⋅105.

Output

For each test case, output on a single line "Yes" if there exists a tree satisfying the given condition, and "No" otherwise.

You may output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "YES", and "yeS" will be recognized as "Yes".

Example

Input

9
6
1 3 4 5 2 6
4
3 4 1 2
5
4 3 5 1 2
4
1 2 3 4
7
4 3 5 7 6 2 1
6
2 4 6 1 3 5
3
2 1 3
4
2 4 1 3
6
4 2 6 5 1 3

Output

Yes
No
No
Yes
No
Yes
Yes
Yes
Yes

Note

In the first example, we can construct the tree with the following edges:

  • {3,1},
  • {4,1},
  • {6,5},
  • {6,2},
  • {6,1}.

Then we have that

  • 1<3, and 1 appears before 3 in 𝑝,
  • 1<4, and 1 appears before 4 in 𝑝,
  • 5<6, and 5 appears before 6 in 𝑝,
  • 2<6, and 2 appears before 6 in 𝑝,
  • 1<6, and 1 appears before 6 in 𝑝.

In the second example, it can be shown that there does not exist a tree satisfying the given constraints.

【尹显齐 25物理学院】我的思路很暴力,就是把能连的边都连上,最后来判断是不是连通图。因为对于一个连通图,总有一棵树能选取里面的若干条边将所有点连通。

但是直接连所有能连通的点肯定会超时,我们的连接方式是:对每一个点,连接它左边和它不连通的比它小的最小数和它右边比它大的最大数。最后,再判断一次是不是所有点都连通即可。

python
class UnionFind:
    def __init__(self,n):
        self.p = list(range(n))
        self.rank = [0]*n
    def find(self,x):
        if self.p[x] != x:
            self.p[x] = self.find(self.p[x])
        return self.p[x]
    def union(self,x,y):
        rx,ry = self.find(x),self.find(y)
        if rx == ry:
            return False
        if self.rank[rx] < self.rank[ry]:
            self.p[rx] = ry
        elif self.rank[rx] > self.rank[ry]:
            self.p[ry] = rx
        else:
            self.p[ry] = rx
            self.rank[rx] += 1
        return True
    def judge(self,x,y):
        return self.find(x) == self.find(y)
for _ in range(int(input())):
    n = int(input())
    arr = list(map(int,input().split()))
    # 前缀最小值最小值预处理
    mins = [arr[0]]
    for i in range(1,n):
        mins.append(min(arr[i],mins[-1]))
    # 后缀最大值
    maxs = [arr[-1]]
    for i in range(n-2,-1,-1):
        maxs.append(max(arr[i],maxs[-1]))
    maxs = maxs[::-1]
    uf = UnionFind(n)
    if maxs[1] > arr[0]:
        uf.union(maxs[1]-1,arr[0]-1)
    for i in range(1,n-1):
        for j in range(1,i+1):
            if mins[i-j] > arr[i]:
                break
            if not uf.judge(mins[i-j]-1,arr[i]-1):
                uf.union(mins[i-j]-1,arr[i]-1)
                break
        if maxs[i+1] > arr[i]:
            uf.union(maxs[i+1]-1,arr[i]-1)
    for j in range(1,n):
        if mins[n-1-j] > arr[n-1]:
            break
        if not uf.judge(mins[n-1-j]-1,arr[n-1]-1):
            uf.union(mins[n-1-j]-1,arr[n-1]-1)
            break
    ans = 1
    for i in range(1,n):
        if not uf.judge(0,i):
            ans = 0
            break
    print("YES" if ans else "NO")