Codeforces Round #637 (Div. 2) - Thanks, Ivan Belonogov! C. Nastya and Strange Generator

Codeforces Round #637 (Div. 2) - Thanks, Ivan Belonogov! C. Nastya and Strange Generator

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≤1e5) — 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≤1e5) — 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 1e5.

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

一道比较有意思的思维题~
各位仔细读题,题意我就不讲了,看样例解释就懂了
仔细观察,我们很快就能发现,如果对当前数 i i i p o s [ i ] pos[i] pos[i](pos[i]表示数 i i i 的位置) 在最右端,那么数 i + 1 i+1 i+1 可以插入到任意位置; 如 果 p o s [ i ] 如果 pos[i] pos[i] 不在最右端,那么数 i + 1 i+1 i+1 一定只能插入到 p o s [ i ] + 1 pos[i]+1 pos[i]+1 的位置。
那么关键就在于判断数 i i i是否在最右端了,我们可以从右往左记录当前位置的最大值,如果当前位置 p o s [ i ] pos[i] pos[i] 的最大值刚好为 i i i,那么数 i i i 就一定在最右端。遍历一遍1到n判断即可,AC代码如下:

#include 
using namespace std;
typedef long long ll;

int main()
{
    int t,n,k;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int p[n+2],r[n+2];
        map<int,int>m;
        for(int i=1;i<=n;i++) cin>>p[i],m[p[i]]=i;
        int mx=0;
        for(int i=n;i>=1;i--)
            mx=max(mx,p[i]),r[i]=mx;
        int flag=1;
        for(int i=1;i<=n;){
            int pos=m[i];
            if(r[pos]==i) i++;
            else{
                if(p[pos+1]==p[pos]+1) i+=2;
                else{
                    flag=0;
                    break;
                }
            }
        }
        if(flag) puts("Yes");
        else puts("No");
    }
    return 0;
}

你可能感兴趣的:(Codeforces,思维)