CF2197B.Array and Permutation

普及-

通过率:0%

时间限制:1.50s

内存限制:256MB

AC君温馨提醒

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

题目描述

Given a permutation pp of length nn∗^{\text{∗}} and an array aa of length nn.

We call the permutation pp generating for the array aa if the array aa can be obtained from the permutation pp by applying some number of operations (possibly zero) of the following type:

Choose an index ii (1≤i<n1 \le i \lt n) and perform one of two replacements:

  • pi:=pi+1p_{i} := p_{i + 1};
  • pi+1:=pip_{i + 1} := p_{i}.

In other words, in one operation, you can choose two adjacent elements of the array and copy the value of one into the other.

You are required to report whether the permutation pp is generating for the array aa.

∗^{\text{∗}}A permutation of length nn 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).

给定一个长度为 nn∗^{\text{∗}} 的排列 pp 和一个长度为 nn 的数组 aa。

若数组 aa 可通过对排列 pp 执行若干次(可以为零次)如下操作得到,则称排列 pp 是数组 aa 的生成排列:

选择一个下标 ii(满足 1≤i<n1 \le i < n),并执行以下两种替换之一:

  • pi:=pi+1p_{i} := p_{i + 1};
  • pi+1:=pip_{i + 1} := p_{i}。

换言之,每次操作中,你可以选择数组中两个相邻元素,并将其中一个的值复制到另一个中。

你需要判断排列 pp 是否是数组 aa 的生成排列。

∗^{\text{∗}} 长度为 nn 的排列是指由 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)。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^{5}) — the length of the array and the permutation.

The second line of each test case contains nn integers p1,p2,…,pnp_1, p_2, \ldots, p_n (1≤pi≤n1 \le p_{i} \le n) — the elements of the permutation.

The third line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_{i} \le n) — the elements of the array.

It is guaranteed that the sum of nn across all test cases does not exceed 2⋅1052 \cdot 10^{5}.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^{5})—— 数组与排列的长度。

每个测试用例的第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \ldots, p_n(1≤pi≤n1 \le p_{i} \le n)—— 排列的元素。

每个测试用例的第三行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \le a_{i} \le n)—— 数组的元素。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^{5}。

输出格式

For each test case, output "YES" if the permutation pp is generating for the array aa, otherwise output "NO".

You may output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.

对于每个测试用例,如果排列 pp 是数组 aa 的生成排列,则输出 "YES";否则输出 "NO"。

你可以以任意大小写形式输出每个字母(小写或大写)。例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 均被视为正确答案。

输入输出样例

  • 输入#1

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

    输出#1

    YES
    NO
    YES
    NO
    YES
    YES

说明/提示

In the first test case, the array is generated from the permutation using one operation:

  • i=2i = 2 and pi+1:=pip_{i + 1} := p_{i}.

In the second test case, it is impossible to obtain aa from pp.

In the third test case, it is necessary to perform 22 operations:

  • i=1i = 1 and pi:=pi+1p_{i} := p_{i + 1};
  • i=2i = 2 and pi+1:=pip_{i + 1} := p_{i}.

在第一个测试用例中,数组是通过对排列执行一次操作生成的:

  • i=2i = 2,且 pi+1:=pip_{i + 1} := p_{i}。

在第二个测试用例中,无法通过排列 pp 得到数组 aa。

在第三个测试用例中,需要执行 22 次操作:

  • i=1i = 1,且 pi:=pi+1p_{i} := p_{i + 1};
  • i=2i = 2,且 pi+1:=pip_{i + 1} := p_{i}。

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

首页