[Solution] Indirect Sort Codeforces Solution
You are given a permutation of size , where each integer from to appears exactly once.
You can do the following operation any number of times (possibly, zero):
- Choose any three indices ().
- If , replace with . Otherwise, swap and .
Determine whether you can make the array sorted in non-descending order.
Each test consists of multiple test cases. The first line contains a single integer () — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer () — the length of the array .
The second line contains integers (, if ) — the elements of the array .
It is guaranteed that the sum of over all test cases does not exceed .
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).
In the first test case, is already sorted in non-descending order.
In the second test case, we can choose . Since , swap and , the array then becomes , which is sorted in non-descending order.
In the seventh test case, we can do the following operations successively:
- Choose . Since , swap and , the array then becomes .
- Choose . Since , replace with , the array then becomes .
- Choose . Since , swap and , the array then becomes .
- Choose . Since , swap and , the array then becomes .
- Choose . Since , swap and , the array then becomes , which is sorted in non-descending order.
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
No comments:
Post a Comment