CF2192E.Swap to Rearrange

提高+/省选-

通过率:0%

时间限制:5.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

You are given 22 arrays, aa and bb, both of length nn. You can perform the following operation:

  • Choose an index ii (1≤i≤n1 \le i \le n) and swap aia_i with bib_i.

You can perform the operation any number of times (possibly zero), but each index can be chosen by at most one operation. Your task is to make aa a rearrangement of bb after all operations, or state that it is impossible. You do not have to minimize the number of operations.

给你两个长度均为 nn 的数组 aa 和 bb。你可以执行以下操作:

  • 选择一个下标 ii(1≤i≤n1 \le i \le n),并交换 aia_i 与 bib_i。

你可以执行该操作任意次数(包括零次),但每个下标最多只能被选择一次。你的任务是在所有操作完成后,使 aa 成为 bb 的一个重排(即两数组元素多重集相同),或者判定这是不可能的。你无需最小化操作次数。

输入格式

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 (1≤n≤1061 \le n \le 10^6).

The second line of each test case contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (1≤ai≤n1 \leq a_i \leq n).

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

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

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

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

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

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

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

输出格式

For each test case, output −1-1 if it is impossible to make aa a rearrangement of bb. Otherwise, output two lines in the following format:

  • In the first line, print the number of operations ss (0≤s≤n0 \leq s \leq n).
  • In the second line of each test case, print ss numbers – the indices you select in each operation in order. You should guarantee that each index is chosen at most once.

If there are multiple possible answers, you many output any.

对于每个测试用例,如果无法使 aa 成为 bb 的一个重排,则输出 −1-1;否则,按以下格式输出两行:

  • 第一行输出操作次数 ss(0≤s≤n0 \leq s \leq n);
  • 每个测试用例的第二行输出 ss 个数字——即每次操作所选的下标(按操作顺序)。需保证每个下标至多被选择一次。

若存在多种可能的答案,输出任意一种即可。

输入输出样例

  • 输入#1

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

    输出#1

    2
    2 4
    -1
    0
    
    2
    3 4

说明/提示

In the first test case, the operations performed are swap(a2,b2a_2, b_2) and swap(a4,b4a_4,b_4), which will make a=[1,2,3,4]a = [1,2,3,4] and b=[2,1,4,3]b = [2,1,4,3]. Now it is possible to achieve aa by rearranging the elements of bb.

In the second test case, it can be shown that no matter what operations we do, we cannot make aa a rearrangement of bb.

在第一个测试用例中,执行的操作是 swap(a2,b2a_2, b_2) 和 swap(a4,b4a_4,b_4),这将使 a=[1,2,3,4]a = [1,2,3,4] 且 b=[2,1,4,3]b = [2,1,4,3]。此时,可以通过重排 bb 的元素来得到 aa。

在第二个测试用例中,可以证明:无论执行何种操作,都无法使 aa 成为 bb 的一个重排。

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

首页