C - Nastya and Strange Generator CodeForces - 1341C

本文介绍了CodeForces上的题目C - Nastya and Strange Generator,解析了题意,并通过示例解释了如何生成满足条件的排列。文章提供了AC代码,帮助理解算法的工作原理。
摘要由CSDN通过智能技术生成

C - Nastya and Strange Generator CodeForces - 1341C

原题网站: 点这里.

题目

Denis was very sad after Nastya rejected him. So he decided to walk through the gateways to have some fun. And luck smiled at him! When he entered the first courtyard, he met a strange man who was selling something.

Denis bought a mysterious item and it was… Random permutation generator! Denis could not believed his luck.

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 occupied 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. 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 asked 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.

题解

看了半天的题,发现题目讲的是真的乱,看了很多的博客才搞明白这个在讲什么,下面分析一下题目:
1、这道题目本身是构造类型的题目,给你一组数据,检测是否符合标准。数据从1到n各随机出现一次,出现规律下面再讲。
2、题目里有提到的几组数组:
r数组—r[i]当前位置i(包含本身)到下一位空格出现时的距离,没有空格则数据为空格。例如数组【 ,2,3,,1】的r数组便是【1,4,4,4,】。
count数组—记录r数组中各个数字出现的次数,例如数组【 ,2,3,,1】的count数组便是【1,0 ,0,3,0】。
初始数组最开始全为空,从1开始到n一次填入。
3.以样例一为模板进行模拟(X表示空格)。

次数i原数组aR数组count数组
0【X,X,X,X,X】【1,2,3,4,5】【1,1,1,1,1】
1【X,X,X,X,1】【1,2,3,4,X】【1,1,1,1,0】
2【2,X,X,X,1】【2,2,3,4,X】【0,2,1,1,0】
3【2,3,X,X,1】【3,3,3,4,X】【0,0,3,1,0】
4【2,3,4,X,1】【4,4,4,4,X】【0,0,0,4,0】
5【2,3,4,5,1】【X,X,X,X,X】【0,0,0,0,0】
分析如下
第一次count全为1,随机选取一个位置填入1,按照题意选择第5个位置
第二次count最大为1,除了第5个位置外全为1,除了第5个位置以外的位置随机选取一个填入2,按照题意选择第1个位置
第三次count最大为2,位于第2位,所以在第2个位置填入3
第四次count最大为3,位于第3位,所以在第3个位置填入4
第五次count最大为4,位于第4位,所以在第4个位置填入5
结束

通过对第一道样例的模拟,能够懂得题意并且发现:
第一个数1都是随机在某个位置产生,当在生成一个数时便会放置到1后面,直到1后面没有空格用于存放数为止,再在1前面的位置随机生成一个数,再在这个数后面添加数。
即对于任意一个数a[i],将其放置于x位置,当其x+1,…,n位置有空格时,a[i+1],a[i+2],a[i+3],…等数将会填充至空格位置,当x后方没有空格时,在1,…,x-1位置上随机位置添加一个数,重复步骤直至所有空格填满。
因此,对于任意一个数a[i],必定有以下规律:
a[i] < a[i - 1] 或者 a[i] - a[i - 1] = 1。

AC代码

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n;
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
		int flag= 1;
		for (int i = 2; i <= n; i++)
			if (a[i] > a[i - 1] && a[i] - a[i - 1] != 1) {
				flag = 0;
				break;
			}
		if (flag == 1)printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值