CF1340A.Nastya and Strange Generator

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

  • For all jj from 11 to nn, we calculate rjr_j — the minimum index such that j≤rj≤nj \leq r_j \leq n, and the position rjr_j is not yet occupied in the permutation. If there are no such positions, then we assume that the value of rjr_j is not defined.
  • For all tt from 11 to nn, we calculate counttcount_t — the number of positions 1≤j≤n1 \leq j \leq n such that rjr_j is defined and rj=tr_j = t.
  • Consider the positions that are still not occupied 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, \times], where ×\times 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. 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,…,pnp_1, p_2, \ldots, p_n 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.

纳斯塔娅拒绝了德尼斯后,他非常伤心。于是他决定穿过一道道门廊散散心。幸运之神眷顾了他!当他进入第一个庭院时,遇到了一位奇怪的男子,正在兜售某种东西。

德尼斯买下了一件神秘物品,结果竟是……一个随机排列生成器!德尼斯简直不敢相信自己的好运。

回到家后,他开始研究这个生成器的工作原理,并弄清了其算法。生成一个排列的过程共包含 nn 步。在第 ii 步(1≤i≤n1 \leq i \leq n),需为数字 ii 选定一个位置。数字 ii 的位置按如下方式确定:

  • 对每个 jj(从 11 到 nn),计算 rjr_j —— 满足 j≤rj≤nj \leq r_j \leq n 且位置 rjr_j 在当前排列中尚未被占据的最小下标;若不存在这样的位置,则认为 rjr_j 未定义。
  • 对每个 tt(从 11 到 nn),计算 counttcount_t —— 满足 1≤j≤n1 \leq j \leq n、rjr_j 有定义且 rj=tr_j = t 的 jj 的个数。
  • 考察当前排列中尚未被占据的位置,在这些位置中,再筛选出在 countcount 数组中对应值最大的位置。
  • 生成器将从上述筛选出的位置中任选一个,作为数字 ii 的位置(即生成器可任意选择)。

我们通过以下示例观察该算法的运行过程:

设 n=5n = 5,且算法已将数字 1,2,31, 2, 3 安排进排列中。现在考察生成器如何为数字 44 选择位置:

  • rr 的取值为 r=[3,3,3,4,×]r = [3, 3, 3, 4, \times],其中 ×\times 表示未定义。
  • 接着 countcount 的取值为 count=[0,0,3,1,0]count = [0, 0, 3, 1, 0]。
  • 当前排列中仅有两个未被占据的位置:33 和 44。位置 33 在 countcount 数组中的值为 33,位置 44 对应的值为 11。
  • 最大值仅在位置 33 处取得,因此算法将唯一地选择该位置放置数字 44。

德尼斯对这次购物十分满意,便回家去了。此后数日,他马不停蹄地生成各种排列。他自信自己构思随机排列的能力不逊于该生成器。接着,他写下脑海中浮现的第一个排列 p1,p2,…,pnp_1, p_2, \ldots, p_n,并想弄清楚:该排列是否可能由该生成器产生?

不幸的是,这个问题对他而言过于困难,于是他向你求助。你需要判断:给定的排列是否可能通过上述算法生成(假设生成器在每一步均恰好选择德尼斯所需的位置)。

输入格式

The first line contains a single integer tt (1≤t≤105)(1 \leq t \leq 10^5) — 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 \leq n \leq 10^5) — the size of the permutation.

The second line of the test case contains nn different integers p1,p2,…,pnp_1, p_2, \ldots, p_n (1≤pi≤n1 \leq p_i \leq n) — the permutation written by Denis.

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

第一行包含一个整数 tt(1≤t≤1051 \leq t \leq 10^5),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \leq n \leq 10^5),表示排列的长度。

每个测试用例的第二行包含 nn 个互不相同的整数 p1,p2,…,pnp_1, p_2, \ldots, p_n(1≤pi≤n1 \leq p_i \leq n),即丹尼斯所写的排列。

保证所有测试用例的 nn 值之和不超过 10510^5。

输出格式

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.

如果该排列可以作为生成器的结果得到,则输出“Yes”;否则输出“No”。

所有字母均可以任意大小写形式显示。

输入输出样例

  • 输入#1

    5
    5
    2 3 4 5 1
    1
    1
    3
    1 3 2
    4
    4 2 3 1
    5
    1 5 2 4 3

    输出#1

    Yes
    Yes
    No
    Yes
    No

说明/提示

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, \times], 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, \times], 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, \times], 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, \times], 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.

我们来模拟第一个测试用例中生成器的运行过程。

第 11 步,r=[1,2,3,4,5]r = [1, 2, 3, 4, 5], count=[1,1,1,1,1]count = [1, 1, 1, 1, 1]。最大值出现在任意未被占用的位置,因此生成器可从位置 11 到 55 中随机选择一个。在本例中,它选择了位置 55。

第 22 步,r=[1,2,3,4,×]r = [1, 2, 3, 4, \times], count=[1,1,1,1,0]count = [1, 1, 1, 1, 0]。最大值出现在位置 11 至 44,因此生成器可在这些位置中随机选择一个。在本例中,它选择了位置 11。

第 33 步,r=[2,2,3,4,×]r = [2, 2, 3, 4, \times], count=[0,2,1,1,0]count = [0, 2, 1, 1, 0]。最大值为 22,且仅在位置 22 处取得,因此生成器将选择该位置。

第 44 步,r=[3,3,3,4,×]r = [3, 3, 3, 4, \times], count=[0,0,3,1,0]count = [0, 0, 3, 1, 0]。最大值为 33,且仅在位置 33 处取得,因此生成器将选择该位置。

第 55 步,r=[4,4,4,4,×]r = [4, 4, 4, 4, \times], count=[0,0,0,4,0]count = [0, 0, 0, 4, 0]。最大值为 44,且仅在位置 44 处取得,因此生成器将选择该位置。

最终,我们得到了排列 2,3,4,5,12, 3, 4, 5, 1,即该生成器有可能生成此排列。

输入解题思路,AI测评打分。不知道怎么写?

首页