#L7989. [USACO21DEC] Bracelet Crossings G
[USACO21DEC] Bracelet Crossings G
CF1658C Shinju and the Lost Permutation
题目描述
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 $ p = [p_{n-i+1}, \ldots, p_n, p_1,p_2, \ldots, p_{n-i}] $ .
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).
输入输出样例 1
6
1
1
2
1 2
2
2 2
6
1 2 4 6 3 5
6
2 3 1 2 3 4
3
3 2 1
YES
YES
NO
NO
YES
NO
说明/提示
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 .