CF2192E.Swap to Rearrange
提高+/省选-
通过率:0%
时间限制:5.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given 2 arrays, a and b, both of length n. You can perform the following operation:
- Choose an index i (1≤i≤n) and swap ai with bi.
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 a a rearrangement of b after all operations, or state that it is impossible. You do not have to minimize the number of operations.
给你两个长度均为 n 的数组 a 和 b。你可以执行以下操作:
- 选择一个下标 i(1≤i≤n),并交换 ai 与 bi。
你可以执行该操作任意次数(包括零次),但每个下标最多只能被选择一次。你的任务是在所有操作完成后,使 a 成为 b 的一个重排(即两数组元素多重集相同),或者判定这是不可能的。你无需最小化操作次数。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤106).
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤n).
The third line of each test case contains n integers b1,b2,…,bn (1≤bi≤n).
It is guaranteed that the sum of n over all test cases does not exceed 106.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤106)。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤n)。
每个测试用例的第三行包含 n 个整数 b1,b2,…,bn(1≤bi≤n)。
保证所有测试用例的 n 值之和不超过 106。
输出格式
For each test case, output −1 if it is impossible to make a a rearrangement of b. Otherwise, output two lines in the following format:
- In the first line, print the number of operations s (0≤s≤n).
- In the second line of each test case, print s 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.
对于每个测试用例,如果无法使 a 成为 b 的一个重排,则输出 −1;否则,按以下格式输出两行:
- 第一行输出操作次数 s(0≤s≤n);
- 每个测试用例的第二行输出 s 个数字——即每次操作所选的下标(按操作顺序)。需保证每个下标至多被选择一次。
若存在多种可能的答案,输出任意一种即可。
输入输出样例
输入#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,b2) and swap(a4,b4), which will make a=[1,2,3,4] and b=[2,1,4,3]. Now it is possible to achieve a by rearranging the elements of b.
In the second test case, it can be shown that no matter what operations we do, we cannot make a a rearrangement of b.
在第一个测试用例中,执行的操作是 swap(a2,b2) 和 swap(a4,b4),这将使 a=[1,2,3,4] 且 b=[2,1,4,3]。此时,可以通过重排 b 的元素来得到 a。
在第二个测试用例中,可以证明:无论执行何种操作,都无法使 a 成为 b 的一个重排。
输入解题思路,AI测评打分。不知道怎么写?