Codeforces Round #637 (Div. 2) C Nastya and Strange Generator

整理的算法模板:ACM算法模板总结(分类详细版)

 

C. Nastya and Strange Generator

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nn steps. At the ii-th step, a place is chosen for the number ii (1≤i≤n)(1≤i≤n). The position for the number ii is defined as follows:

  • For all jj from 11 to nn, we calculate rjrj  — the minimum index such that j≤rj≤nj≤rj≤n, and the position rjrj is not yet occupied in the permutation. If there are no such positions, then we assume that the value of rjrj is not defined.
  • For all tt from 11 to nn, we calculate counttcountt  — the number of positions 1≤j≤n1≤j≤n such that rjrj is defined and rj=trj=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 countcount array is maximum.
  • The generator selects one of these positions for the number ii. The generator can choose any position.

Let's have a look at the operation of the algorithm in the following example:

 

Let n=5n=5 and the algorithm has already arranged the numbers 1,2,31,2,3 in the permutation. Consider how the generator will choose a position for the number 44:

  • The values of rr will be r=[3,3,3,4,×]r=[3,3,3,4,×], where ×× means an indefinite value.
  • Then the countcount values will be count=[0,0,3,1,0]count=[0,0,3,1,0].
  • There are only two unoccupied positions in the permutation: 33 and 44. The value in the countcount array for position 33 is 33, for position 44 it is 11.
  • The maximum value is reached only for position 33, so the algorithm will uniquely select this position for number 44.

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,…,pnp1,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 tt (1≤t≤105)(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 nn (1≤n≤105)(1≤n≤105)  — the size of the permutation.

The second line of the test case contains nn different integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n)  — the permutation written by Denis.

It is guaranteed that the sum of nn over all test cases doesn't exceed 105105.

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 11 step, r=[1,2,3,4,5],count=[1,1,1,1,1]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 11 to 55. In our example, it chose 55.

At the 22 step, r=[1,2,3,4,×],count=[1,1,1,1,0]r=[1,2,3,4,×],count=[1,1,1,1,0]. The maximum value is reached in positions from 11 to 44, so the generator can choose a random position among them. In our example, it chose 11.

At the 33 step, r=[2,2,3,4,×],count=[0,2,1,1,0]r=[2,2,3,4,×],count=[0,2,1,1,0]. The maximum value is 22 and is reached only at the 22 position, so the generator will choose this position.

At the 44 step, r=[3,3,3,4,×],count=[0,0,3,1,0]r=[3,3,3,4,×],count=[0,0,3,1,0]. The maximum value is 33 and is reached only at the 33 position, so the generator will choose this position.

At the 55 step, r=[4,4,4,4,×],count=[0,0,0,4,0]r=[4,4,4,4,×],count=[0,0,0,4,0]. The maximum value is 44 and is reached only at the 44 position, so the generator will choose this position.

In total, we got a permutation of 2,3,4,5,12,3,4,5,1, that is, a generator could generate it.

 

(从C题看出来这真是阅读理解专场啊.....)

每次生成之前先根据前面已经生成的数来统计一下每个未生成的数的权值count,再根据count数组来选择。
假设要生成第i个数(已经生成了i-1个了)count数组全部先赋0,然后对于i-1个已经生成过的数j向后找第一个未生成的数rj,然后count[rj]++,选择最大的count[k],k就可以是第i个随机数组的数了(如果k有多个,选择任意一个都可以)。题目要求是给你一个数组然后判断根据上面的规则是否可以生成这个数组。

思路:可以发现随机数组是一个1到n全排列(这个其实没啥用处),生成第一个数的时候count数组全为0,随意第一个数可以任意选,假设选择了ans[1],接下来选择第二个数,count数组只有ans[1]+1是1其余全为0(所以j=ans[1]时,rj=ans[1]+1),这样第二个数只能选择ans[1]+1(ans[2]=ans[1]+1),接下来第三个数count只有ans[1]+2是2,其他全为0(j=ans[1]和j=ans[1]+1使rj都=ans[1]+2),ans[3]=ans[1]+2…是不是发现规律了,这几个数是依次递增的,直到ans[i]==n时才会跳出,这个时候ans[1]后面的数都选择完了,只能选择ans[1]前的数了,这个时候可以把n看做ans[1]-1不就又可以继续上面的操作了嘛直到n个数选完。

就是说我们有一个[l,r]的区间,当前一个选择任意一个数,就相当于选择一个数dl将区间[l,r]划分成了两个区间[l,dl-1]和[dl,r],划分过后就只能依次选择区间[dl,r]的数了(必须从小到大),直到这个区间[dl,r]被选择完才可以跳出,跳出之后就令r=dl-1,继续上面的操作,直到n个数都被选择就结束了,如果满足这个过程就是可以生成这样的随机数组,否则就是不能。

(转自https://blog.csdn.net/qq_45288870/article/details/105725181)

其实我做的时候看样例看出来的规律就是,数列里面每个数要么比后一个数大   要么比后一个数小1

 

#include 
using namespace std;
int a[100005];
int main()
{
    int t;
    cin >>t;
    while(t--)
    {
        int n;
        cin >>n;
        for(int i=0;i>a[i];
        }
        int flag=1;
        for(int i=0;i1)
            {
                flag=0;
                break;
            }
        }
        if(flag) cout <<"Yes"<

 

你可能感兴趣的:(基础算法——前缀和)