You are given a permutation a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an of size n n n , where each integer from 1 1 1 to n n n appears exactly once.
You can do the following operation any number of times (possibly, zero):
Determine whether you can make the array a a a sorted in non-descending order.
Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 5000 1 \le t \le 5000 1≤t≤5000 ) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n n n ( 3 ≤ n ≤ 10 3 \le n \le 10 3≤n≤10 ) — the length of the array a a a .
The second line contains n n n integers a 1 , a 2 , … , a n a_1,a_2,\dots,a_n a1,a2,…,an( 1 ≤ a i ≤ n 1 \le a_i \le n 1≤ai≤n , a i ≠ a j a_i \neq a_j ai=aj if i ≠ j i \neq j i=j ) — the elements of the array a a a .
For each test case, output “Yes” (without quotes) if the array can be sorted in non-descending order, and “No” (without quotes) otherwise.
You can output “Yes” and “No” in any case (for example, strings “YES”, “yEs” and “yes” will be recognized as a positive response).
7
3
1 2 3
3
1 3 2
7
5 3 4 7 6 2 1
7
7 6 5 4 3 2 1
5
2 1 4 5 3
5
2 1 3 4 5
7
1 2 6 7 4 3 5
Yes
Yes
No
No
No
No
Yes
In the first test case, [ 1 , 2 , 3 ] [1,2,3] [1,2,3] is already sorted in non-descending order.
In the second test case, we can choose i = 1 , j = 2 , k = 3 i = 1,j = 2,k = 3 i=1,j=2,k=3 . Since a 1 ≤ a 3 a_1 \le a_3 a1≤a3 , swap a 2 a_2 a2 and a 3 a_3 a3 , the array then becomes [ 1 , 2 , 3 ] [1,2,3] [1,2,3] , which is sorted in non-descending order.
In the seventh test case, we can do the following operations successively:
In the third, the fourth, the fifth and the sixth test cases, it can be shown that the array cannot be sorted in non-descending order.
#include
using namespace std;
int main() {
int t,n,a,b,i;
cin>>t;
while(t--) {
cin>>n>>b;
for(i=2;i<=n;i++)
cin>>a;
if(b==1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}