Being upset after this behavior of Nastya, Denis was very sad. Nothing could make the rejected guy happier. To at least somehow have fun, he decided to walk through the gateways. And, luck smiled at him! When he entered the first courtyard, he met a strange a man who was selling something.
Looking around, Denis went to the stranger and bought a mysterious product. It was… Random permutation generator! That is what the boy has been looking for so long!
When he arrived home, he began to study how his generator works and learned the algorithm. The process of generating a permutation consists of n steps. At the i-th step, a place is chosen for the number i (1≤i≤n). The position for the number i is defined as follows:
For all j from 1 to n, we calculate rj — the minimum index such that j≤rj≤n, and the position rj is not yet occupied in the permutation. If there are no such positions, then we assume that the value of rj is not defined.
For all t from 1 to n, we calculate countt — the number of positions 1≤j≤n such that rj is defined and rj=t.
Consider the positions that are still not taken up by permutation and among those we consider the positions for which the value in the count array is maximum.
The generator selects one of these positions for the number i. The generator can choose any position.
Let’s have a look at the operation of the algorithm in the following example:
Let n=5 and the algorithm has already arranged the numbers 1,2,3 in the permutation. Consider how the generator will choose a position for the number 4:
The values of r will be r=[3,3,3,4,×], where × means an indefinite value.
Then the count values will be count=[0,0,3,1,0].
There are only two unoccupied positions in the permutation: 3 and 4. The value in the count array for position 3 is 3, for position 4 it is 1.
The maximum value is reached only for position 3, so the algorithm will uniquely select this position for number 4.
Satisfied with his purchase, Denis went home. For several days without a break, he generated permutations and decided that he was filled with awareness of the generation process. He believes that he can come up with random permutations no worse than a generator. After that, he wrote out the first permutation that came to mind p1,p2,…,pn and decided to find out if it could be obtained as a result of the generator.
Unfortunately, this task was too difficult for him, and he turned to you for help. It is necessary to define whether the written permutation could be obtained using the described algorithm if the generator always selects the position Denis needs.
Input
The first line contains a single integer t (1≤t≤105) — the number of test cases. Then the descriptions of the test cases follow.
The first line of the test case contains a single integer n (1≤n≤105) — the size of the permutation.
The second line of the test case contains n different integers p1,p2,…,pn (1≤pi≤n) — the permutation written by Denis.
It is guaranteed that the sum of n over all test cases doesn’t exceed 105.
Output
Print “Yes” if this permutation could be obtained as a result of the generator. Otherwise, print “No”.
All letters can be displayed in any case.
Example
Input
5
5
2 3 4 5 1
1
1
3
1 3 2
4
4 2 3 1
5
1 5 2 4 3
Output
Yes
Yes
No
Yes
No
Note
Let’s simulate the operation of the generator in the first test.
At the 1 step, r=[1,2,3,4,5],count=[1,1,1,1,1]. The maximum value is reached in any free position, so the generator can choose a random position from 1 to 5. In our example, it chose 5.
At the 2 step, r=[1,2,3,4,×],count=[1,1,1,1,0]. The maximum value is reached in positions from 1 to 4, so the generator can choose a random position among them. In our example, it chose 1.
At the 3 step, r=[2,2,3,4,×],count=[0,2,1,1,0]. The maximum value is 2 and is reached only at the 2 position, so the generator will choose this position.
At the 4 step, r=[3,3,3,4,×],count=[0,0,3,1,0]. The maximum value is 3 and is reached only at the 3 position, so the generator will choose this position.
At the 5 step, r=[4,4,4,4,×],count=[0,0,0,4,0]. The maximum value is 4 and is reached only at the 4 position, so the generator will choose this position.
In total, we got a permutation of 2,3,4,5,1, that is, a generator could generate it.
思路:这个题目,只要第一个确定了,后面的也就确定了,除非后面的指向n+1这个位置。如果后面指向n+1这个位置,那么剩下的就可以根据下一个数来确定。利用并查集确定下一个指向的位置。
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=1e5+100;
int a[maxx];
int pos[maxx];
int f[maxx];
int n;
inline void init()
{
for(int i=1;i<=n+1;i++) f[i]=i;
}
inline int getf(int u)
{
return u==f[u]?u:f[u]=getf(f[u]);
}
inline void merge(int u,int v)
{
int t1=getf(u);
int t2=getf(v);
if(t1!=t2) f[t1]=t2;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
init();
for(int i=1;i<=n;i++) scanf("%d",&a[i]),pos[a[i]]=i;
int flag=1;
int _max;
for(int i=1;i<=n;i++)
{
if(i>1)
{
if(_max!=pos[i]&&_max!=n+1)
{
flag=0;
break;
}
}
merge(pos[i],pos[i]+1);
_max=getf(pos[i]);
}
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
努力加油a啊,(o)/~