题目收集
题目列表题单
返回列表

Disjoint LIS and LDS

CodeforcesP0013· ZJU Summer 2026 Contest 1原题链接
哈希adhoc

创建于 2026/07/16 · 更新于 2026/07/16

题目描述

You are given a permutation ppp of length nnn.

A subsequence of ppp is obtained by deleting zero or more elements from ppp without changing the order of the remaining elements.

A subsequence is called increasing if its elements are strictly increasing, and decreasing if its elements are strictly decreasing.

A longest increasing subsequence, abbreviated as LIS, is an increasing subsequence with the maximum possible length among all increasing subsequences of ppp. Similarly, a longest decreasing subsequence, abbreviated as LDS, is a decreasing subsequence with the maximum possible length among all decreasing subsequences of ppp.

Determine whether there exist an LIS and an LDS of ppp such that they are disjoint, i.e. they do not use the same position of the original permutation.

输入格式

The first line contains an integer TTT (1≤T≤5⋅1051 \le T \le 5 \cdot 10^51≤T≤5⋅105), the number of test cases.

Each test case consists of two lines.

The first line of each test case contains an integer nnn (1≤n≤5⋅1051 \le n \le 5 \cdot 10^51≤n≤5⋅105), the length of the permutation.

The second line contains nnn integers p1,p2,…,pnp_1, p_2, \ldots, p_np1​,p2​,…,pn​ (1≤pi≤n1 \le p_i \le n1≤pi​≤n), forming a permutation of 1,2,…,n1, 2, \ldots, n1,2,…,n.

It is guaranteed that the sum of nnn over all test cases does not exceed 5⋅1055 \cdot 10^55⋅105.

输出格式

For each test case, output YES if there exist a longest increasing subsequence and a longest decreasing subsequence that are disjoint.

Otherwise, output NO.

You may print each letter in any case. For example, YES, Yes, and yes are all accepted.

样例输入 #1

6
1
1
4
2 4 1 3
4
2 1 4 3
5
1 2 3 4 5
5
5 4 3 2 1
8
3 7 1 5 8 2 6 4

样例输出 #1

NO
YES
NO
NO
NO
YES