#P1658C. Shinju and the Lost Permutation
Shinju and the Lost Permutation
Description
Shinju loves permutations very much! Today, she has borrowed a permutation from Juju to play with.
The -th cyclic shift of a permutation is a transformation on the permutation such that will now become .
Let's define the power of permutation as the number of distinct elements in the prefix maximums array of the permutation. The prefix maximums array is the array of length such that . For example, the power of is since and there are distinct elements in .
Unfortunately, Shinju has lost the permutation ! The only information she remembers is an array , where is the power of the -th cyclic shift of the permutation . She's also not confident that she remembers it correctly, so she wants to know if her memory is good enough.
Given the array , determine if there exists a permutation that is consistent with . You do not have to construct the permutation .
A permutation is an array consisting of distinct integers from to in arbitrary order. For example, is a permutation, but is not a permutation ( appears twice in the array) and is also not a permutation ( but there is in the array).
The input consists of multiple test cases. The first line contains a single integer () — the number of test cases.
The first line of each test case contains an integer ().
The second line of each test case contains integers ().
It is guaranteed that the sum of over all test cases does not exceed .
For each test case, print "YES" if there is a permutation exists that satisfies the array , and "NO" otherwise.
You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
Input
The input consists of multiple test cases. The first line contains a single integer () — the number of test cases.
The first line of each test case contains an integer ().
The second line of each test case contains integers ().
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, print "YES" if there is a permutation exists that satisfies the array , and "NO" otherwise.
You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
Note
In the first test case, the permutation satisfies the array .
In the second test case, the permutation satisfies the array .
In the fifth test case, the permutation satisfies the array . Let's see why this is true.
- The zeroth cyclic shift of is . Its power is since and there are distinct elements — and .
- The first cyclic shift of is . Its power is since .
- The second cyclic shift of is . Its power is since .
- The third cyclic shift of is . Its power is since .
- The fourth cyclic shift of is . Its power is since .
- The fifth cyclic shift of is . Its power is since .
Therefore, .
In the third, fourth, and sixth testcases, we can show that there is no permutation that satisfies array .