CF1698F.Equal Reversal

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There is an array aa of length nn. You may perform the following operation on it:

  • Choose two indices ll and rr where 1≤l≤r≤n1 \le l \le r \le n and al=ara_l = a_r. Then, reverse the subsegment from the ll-th to the rr-th element, i. e. set [al,al+1,…,ar−1,ar][a_l, a_{l + 1}, \ldots, a_{r - 1}, a_r] to [ar,ar−1,…,al+1,al][a_r, a_{r-1}, \ldots, a_{l+1}, a_l].

You are also given another array bb of length nn which is a permutation of aa. Find a sequence of at most n2n^2 operations that transforms array aa into bb, or report that no such sequence exists.

有一个长度为 nn 的数组 aa。你可以对它执行以下操作:

  • 选择两个下标 ll 和 rr,满足 1≤l≤r≤n1 \le l \le r \le n 且 al=ara_l = a_r。然后,将从第 ll 个到第 rr 个元素构成的子段进行翻转,即把 [al,al+1,…,ar−1,ar][a_l, a_{l + 1}, \ldots, a_{r - 1}, a_r] 变为 [ar,ar−1,…,al+1,al][a_r, a_{r-1}, \ldots, a_{l+1}, a_l]。

同时给你另一个长度为 nn 的数组 bb,它是 aa 的一个排列。请找出一个最多包含 n2n^2 次操作的序列,使得将数组 aa 变为 bb;若不存在这样的操作序列,则报告无解。

输入格式

Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1001 \leq t \leq 100) — 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≤5001 \le n \le 500) — 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 500500.

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

每个测试用例的第一行包含一个整数 nn(1≤n≤5001 \le n \le 500),表示数组 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 的总和不超过 500500。

输出格式

For each test case, output "NO" (without quotes) if it is impossible to turn aa into bb using at most n2n^2 operations.

Otherwise, output "YES" (without quotes). Then output an integer kk (0≤k≤n20 \leq k \leq n^2) denoting the number of operations you will perform. Note that you don't have to minimize the number of operations.

Afterwards, output kk lines. The ii-th line should contain two integers lil_i and rir_i (1≤li≤ri≤n1 \leq l_i \leq r_i \leq n) — the left and right indices for the ii-th operation.

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

If there are multiple possible sequences of operations, you may output any of them.

对于每个测试用例,若无法在至多 n2n^2 次操作内将 aa 变为 bb,则输出 "NO"(不带引号)。

否则,输出 "YES"(不带引号)。接着输出一个整数 kk(0≤k≤n20 \leq k \leq n^2),表示你将执行的操作次数。注意:你无需最小化操作次数。

随后,输出 kk 行。第 ii 行应包含两个整数 lil_i 和 rir_i(1≤li≤ri≤n1 \leq l_i \leq r_i \leq n)——即第 ii 次操作的左端点和右端点。

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

若存在多种可能的操作序列,你可以输出其中任意一种。

输入输出样例

  • 输入#1

    5
    8
    1 2 4 3 1 2 1 1
    1 1 3 4 2 1 2 1
    7
    1 2 3 1 3 2 3
    1 3 2 3 1 2 3
    3
    1 1 2
    1 2 1
    2
    1 2
    2 1
    1
    1
    1

    输出#1

    YES
    2
    5 8
    1 6
    YES
    2
    1 4
    3 6
    NO
    NO
    YES
    0

说明/提示

In the first test case, we can perform the following operations: $$[1,2,4,3,1,2,1,1] \xrightarrow[l=5,\,r=8]{} [1,2,4,3,1,1,2,1] \xrightarrow[l=1,\,r=6]{} [1,1,3,4,2,1,2,1].$$

In the second test case, we can perform the following operations: $$[1,2,3,1,3,2,3] \xrightarrow[l=1,\,r=4]{} [1,3,2,1,3,2,3] \xrightarrow[l=3,\,r=6]{} [1,3,2,3,1,2,3].$$

It can be proven that it is impossible to turn aa into bb in the third and fourth test cases.

在第一个测试用例中,我们可以执行以下操作:

[1,2,4,3,1,2,1,1]→l=5, r=8[1,2,4,3,1,1,2,1]→l=1, r=6[1,1,3,4,2,1,2,1].[1,2,4,3,1,2,1,1] \xrightarrow[l=5,\,r=8]{} [1,2,4,3,1,1,2,1] \xrightarrow[l=1,\,r=6]{} [1,1,3,4,2,1,2,1].

在第二个测试用例中,我们可以执行以下操作:

[1,2,3,1,3,2,3]→l=1, r=4[1,3,2,1,3,2,3]→l=3, r=6[1,3,2,3,1,2,3].[1,2,3,1,3,2,3] \xrightarrow[l=1,\,r=4]{} [1,3,2,1,3,2,3] \xrightarrow[l=3,\,r=6]{} [1,3,2,3,1,2,3].

可以证明,在第三和第四个测试用例中,无法将 aa 变为 bb。

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

首页