CF1658C.Shinju and the Lost Permutation

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Shinju loves permutations very much! Today, she has borrowed a permutation pp from Juju to play with.

The ii-th cyclic shift of a permutation pp is a transformation on the permutation such that $p = [p_1, p_2, \ldots, p_n] $ will now become $ p = [p_{n-i+1}, \ldots, p_n, p_1,p_2, \ldots, p_{n-i}]$.

Let's define the power of permutation pp as the number of distinct elements in the prefix maximums array bb of the permutation. The prefix maximums array bb is the array of length nn such that bi=max⁡(p1,p2,…,pi)b_i = \max(p_1, p_2, \ldots, p_i). For example, the power of [1,2,5,4,6,3][1, 2, 5, 4, 6, 3] is 44 since b=[1,2,5,5,6,6]b=[1,2,5,5,6,6] and there are 44 distinct elements in bb.

Unfortunately, Shinju has lost the permutation pp! The only information she remembers is an array cc, where cic_i is the power of the (i−1)(i-1)-th cyclic shift of the permutation pp. She's also not confident that she remembers it correctly, so she wants to know if her memory is good enough.

Given the array cc, determine if there exists a permutation pp that is consistent with cc. You do not have to construct the permutation pp.

A permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3, 4] is also not a permutation (n=3n=3 but there is 44 in the array).

Shinju 非常喜欢排列!今天,她向 Juju 借来一个排列 pp 来玩耍。

排列 pp 的第 ii 个循环移位(cyclic shift)是一种对排列的变换,使得 p=[p1,p2,…,pn]p = [p_1, p_2, \ldots, p_n] 变为 p=[pn−i+1,…,pn,p1,p2,…,pn−i]p = [p_{n-i+1}, \ldots, p_n, p_1, p_2, \ldots, p_{n-i}]。

我们定义排列 pp 的**幂值(power)**为该排列的前缀最大值数组 bb 中不同元素的个数。前缀最大值数组 bb 是一个长度为 nn 的数组,满足 bi=max⁡(p1,p2,…,pi)b_i = \max(p_1, p_2, \ldots, p_i)。例如,排列 [1,2,5,4,6,3][1, 2, 5, 4, 6, 3] 的幂值为 44,因为其前缀最大值数组为 b=[1,2,5,5,6,6]b = [1, 2, 5, 5, 6, 6],其中包含 44 个不同的元素。

不幸的是,Shinju 弄丢了排列 pp!她唯一记得的信息是一个数组 cc,其中 cic_i 表示排列 pp 的第 (i−1)(i-1) 个循环移位的幂值。但她对自己记忆的准确性并不确信,因此她想知道自己的记忆是否足够可靠。

给定数组 cc,请判断是否存在一个排列 pp,使得其所有循环移位的幂值恰好构成数组 cc。你不需要构造出这样的排列 pp。

排列是指由 11 到 nn 的 nn 个互不相同的整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数字 22 出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中出现了 44)。

输入格式

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤5⋅1031 \leq t \leq 5 \cdot 10^3) — the number of test cases.

The first line of each test case contains an integer nn (1≤n≤1051 \le n \le 10^5).

The second line of each test case contains nn integers c1,c2,…,cnc_1,c_2,\ldots,c_n (1≤ci≤n1 \leq c_i \leq n).

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤5⋅1031 \leq t \leq 5 \cdot 10^3),表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5)。

每个测试用例的第二行包含 nn 个整数 c1,c2,…,cnc_1,c_2,\ldots,c_n(1≤ci≤n1 \leq c_i \leq n)。

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

输出格式

For each test case, print "YES" if there is a permutation pp exists that satisfies the array cc, and "NO" otherwise.

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).

对于每个测试用例,如果存在一个排列 pp 满足数组 cc,则输出 "YES";否则输出 "NO"。

你可以以任意大小写形式输出 "YES" 和 "NO"(例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 均被视为肯定回答)。

输入输出样例

  • 输入#1

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

    输出#1

    YES
    YES
    NO
    NO
    YES
    NO

说明/提示

In the first test case, the permutation [1][1] satisfies the array cc.

In the second test case, the permutation [2,1][2,1] satisfies the array cc.

In the fifth test case, the permutation [5,1,2,4,6,3][5, 1, 2, 4, 6, 3] satisfies the array cc. Let's see why this is true.

  • The zeroth cyclic shift of pp is [5,1,2,4,6,3][5, 1, 2, 4, 6, 3]. Its power is 22 since b=[5,5,5,5,6,6]b = [5, 5, 5, 5, 6, 6] and there are 22 distinct elements — 55 and 66.
  • The first cyclic shift of pp is [3,5,1,2,4,6][3, 5, 1, 2, 4, 6]. Its power is 33 since b=[3,5,5,5,5,6]b=[3,5,5,5,5,6].
  • The second cyclic shift of pp is [6,3,5,1,2,4][6, 3, 5, 1, 2, 4]. Its power is 11 since b=[6,6,6,6,6,6]b=[6,6,6,6,6,6].
  • The third cyclic shift of pp is [4,6,3,5,1,2][4, 6, 3, 5, 1, 2]. Its power is 22 since b=[4,6,6,6,6,6]b=[4,6,6,6,6,6].
  • The fourth cyclic shift of pp is [2,4,6,3,5,1][2, 4, 6, 3, 5, 1]. Its power is 33 since b=[2,4,6,6,6,6]b = [2, 4, 6, 6, 6, 6].
  • The fifth cyclic shift of pp is [1,2,4,6,3,5][1, 2, 4, 6, 3, 5]. Its power is 44 since b=[1,2,4,6,6,6]b = [1, 2, 4, 6, 6, 6].

Therefore, c=[2,3,1,2,3,4]c = [2, 3, 1, 2, 3, 4].

In the third, fourth, and sixth testcases, we can show that there is no permutation that satisfies array cc.

在第一个测试用例中,排列 [1][1] 满足数组 cc。

在第二个测试用例中,排列 [2,1][2,1] 满足数组 cc。

在第五个测试用例中,排列 [5,1,2,4,6,3][5, 1, 2, 4, 6, 3] 满足数组 cc。下面我们说明其正确性。

  • pp 的第零次循环移位为 [5,1,2,4,6,3][5, 1, 2, 4, 6, 3]。其“幂”为 22,因为此时 b=[5,5,5,5,6,6]b = [5, 5, 5, 5, 6, 6],其中包含 22 个不同元素——55 和 66。
  • pp 的第一次循环移位为 [3,5,1,2,4,6][3, 5, 1, 2, 4, 6]。其“幂”为 33,因为此时 b=[3,5,5,5,5,6]b=[3,5,5,5,5,6]。
  • pp 的第二次循环移位为 [6,3,5,1,2,4][6, 3, 5, 1, 2, 4]。其“幂”为 11,因为此时 b=[6,6,6,6,6,6]b=[6,6,6,6,6,6]。
  • pp 的第三次循环移位为 [4,6,3,5,1,2][4, 6, 3, 5, 1, 2]。其“幂”为 22,因为此时 b=[4,6,6,6,6,6]b=[4,6,6,6,6,6]。
  • pp 的第四次循环移位为 [2,4,6,3,5,1][2, 4, 6, 3, 5, 1]。其“幂”为 33,因为此时 b=[2,4,6,6,6,6]b = [2, 4, 6, 6, 6, 6]。
  • pp 的第五次循环移位为 [1,2,4,6,3,5][1, 2, 4, 6, 3, 5]。其“幂”为 44,因为此时 b=[1,2,4,6,6,6]b = [1, 2, 4, 6, 6, 6]。

因此,c=[2,3,1,2,3,4]c = [2, 3, 1, 2, 3, 4]。

在第三个、第四个和第六个测试用例中,可以证明不存在满足数组 cc 的排列。

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

首页