CF1672D.Cyclic Rotation

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There is an array aa of length nn. You may perform the following operation any number of times:

  • Choose two indices ll and rr where 1≤l<r≤n1 \le l \lt r \le n and al=ara_l = a_r. Then, set a[l…r]=[al+1,al+2,…,ar,al]a[l \ldots r] = [a_{l+1}, a_{l+2}, \ldots, a_r, a_l].

You are also given another array bb of length nn which is a permutation of aa. Determine whether it is possible to transform array aa into an array bb using the above operation some number of times.

有一个长度为 nn 的数组 aa。你可以执行以下操作任意多次:

  • 选择两个下标 ll 和 rr,满足 1≤l<r≤n1 \le l \lt r \le n 且 al=ara_l = a_r。然后,将子数组 a[l…r]a[l \ldots r] 替换为 [al+1,al+2,…,ar,al][a_{l+1}, a_{l+2}, \ldots, a_r, a_l]。

你还会得到另一个长度为 nn 的数组 bb,它是 aa 的一个排列。判断是否可以通过若干次上述操作将数组 aa 变为数组 bb。

输入格式

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

The first line of each test case contains an integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10 ^ 5) — the length of array aa and bb.

The second 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) — elements of the array aa.

The third line of each test case contains nn integers b1,b2,…,bnb_1, b_2, \ldots, b_n (1≤bi≤n1 \le b_i \le n) — elements of the array bb.

It is guaranteed that bb is a permutation of aa.

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

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

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10 ^ 5),表示数组 aa 和 bb 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \le a_i \le n),表示数组 aa 的元素。

每个测试用例的第三行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \ldots, b_n(1≤bi≤n1 \le b_i \le n),表示数组 bb 的元素。

保证 bb 是 aa 的一个排列。

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

输出格式

For each test case, print "YES" (without quotes) if it is possible to transform array aa to bb, and "NO" (without quotes) otherwise.

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

对于每个测试用例,如果可以将数组 aa 变换为 bb,则输出 "YES"(不带引号);否则输出 "NO"(不带引号)。

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

输入输出样例

  • 输入#1

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

    输出#1

    YES
    YES
    NO
    YES
    NO

说明/提示

In the first test case, we can choose l=2l=2 and r=5r=5 to form [1,3,3,2,2][1, 3, 3, 2, 2].

In the second test case, we can choose l=2l=2 and r=4r=4 to form [1,4,2,2,1][1, 4, 2, 2, 1]. Then, we can choose l=1l=1 and r=5r=5 to form [4,2,2,1,1][4, 2, 2, 1, 1].

In the third test case, it can be proven that it is not possible to transform array aa to bb using the operation.

在第一个测试用例中,我们可以选择 l=2l=2 和 r=5r=5,得到数组 [1,3,3,2,2][1, 3, 3, 2, 2]。

在第二个测试用例中,我们可以先选择 l=2l=2 和 r=4r=4,得到数组 [1,4,2,2,1][1, 4, 2, 2, 1];然后选择 l=1l=1 和 r=5r=5,得到数组 [4,2,2,1,1][4, 2, 2, 1, 1]。

在第三个测试用例中,可以证明:无法通过该操作将数组 aa 变换为数组 bb。

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

首页