P16728 [CodeChef] CONCATSORT
提高-
题目描述

JJ has an array A. He can perform the following operation on A:
Divide A into two subsequences P and Q such that each A_{i} belongs to either P or Q.
Set A := P\ $\texttt{concat}$\ Q

Here $\texttt{concat}$ denotes the concatenation operation. For $e.g. [2, 1, 5] \texttt{ concat } [4, 3] = [2, 1, 5, 4, 3]$.

Is it possible to make A sorted in non-decreasing order after applying the above operation at most once?

Note: An array X is a subsequence of an array Y if X can be obtained by deletion of several (possibly, zero or all) elements from Y.

------ Input Format ------

  • The first line contains a single integer T — the number of test cases. Then the test cases follow.
  • The first line of each test case contains an integer N — the size of the array A.
  • The second line of each test case contains $N space-separated integers A_{1}, A_{2}, \ldots, A_{N}$ denoting the array A.

------ Output Format ------

For each test case, output YES if it is possible to make the array A sorted after applying the given operation at most once. Otherwise, output NO.

You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical).

------ Constraints ------

$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.

----- Sample Input 1 ------
3
6
4 5 6 1 2 3
5
1 3 5 2 4
5
5 10 7 11 9

----- Sample Output 1 ------
YES
NO
YES

----- explanation 1 ------
Test case $1$: We can select $P = [1, 2, 3]$ and $Q = [4, 5, 6]$. Therefore $A$ will become $[1, 2, 3, 4, 5, 6]$ which is sorted.

Test case $2$: It can be proven that it is not possible to sort $A$ by using the given operation at most once.

Test case $3$: We can select $P = [5, 7, 9]$ and $Q = [10, 11]$. Therefore $A$ will become $[5, 7, 9, 10, 11]$ which is sorted.

输入格式

见题面

输出格式

见题面

样例输入
3 6 4 5 6 1 2 3 5 1 3 5 2 4 5 5 10 7 11 9
样例输出
YES NO YES