CF1681C.Double Sort

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given two arrays aa and bb, both consisting of nn integers.

In one move, you can choose two indices ii and jj (1≤i,j≤n1 \le i, j \le n; i≠ji \neq j) and swap aia_i with aja_j and bib_i with bjb_j. You have to perform the swap in both arrays.

You are allowed to perform at most 10410^4 moves (possibly, zero). Can you make both arrays sorted in a non-decreasing order at the end? If you can, print any sequence of moves that makes both arrays sorted.

给你两个数组 aa 和 bb,它们均包含 nn 个整数。

在一次操作中,你可以选择两个下标 ii 和 jj(满足 1≤i,j≤n1 \le i, j \le n 且 i≠ji \neq j),并同时交换 aia_i 与 aja_j,以及 bib_i 与 bjb_j。即:必须在两个数组中同步执行该交换。

你最多可以执行 10410^4 次操作(也可以不执行任何操作)。你能否通过若干次操作,使得两个数组最终都按非递减顺序排列?如果可以,请输出任意一组实现该目标的操作序列。

输入格式

The first line contains a single integer tt (1≤t≤1001 \le t \le 100) — the number of testcases.

The first line of each testcase contains a single integer nn (2≤n≤1002 \le n \le 100) — the number of elements in both arrays.

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤n1 \le a_i \le n) — the first array.

The third line contains nn integers b1,b2,…,bnb_1, b_2, \dots, b_n (1≤bi≤n1 \le b_i \le n) — the second array.

第一行包含一个整数 tt(1≤t≤1001 \le t \le 100)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(2≤n≤1002 \le n \le 100)—— 两个数组中元素的个数。

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

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

输出格式

For each testcase, print the answer. If it's impossible to make both arrays sorted in a non-decreasing order in at most 10410^4 moves, print -1. Otherwise, first, print the number of moves kk (0≤k≤104)(0 \le k \le 10^4). Then print ii and jj for each move (1≤i,j≤n(1 \le i, j \le n; i≠j)i \neq j).

If there are multiple answers, then print any of them. You don't have to minimize the number of moves.

对于每个测试用例,输出答案。如果无法在至多 10410^4 次操作内使两个数组均变为非递减顺序,则输出 -1。否则,首先输出操作次数 kk(其中 0≤k≤1040 \le k \le 10^4);然后对每次操作,输出对应的 ii 和 jj(其中 1≤i,j≤n1 \le i, j \le n,且 i≠ji \neq j)。

若存在多个合法答案,输出任意一个即可。你无需最小化操作次数。

输入输出样例

  • 输入#1

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

    输出#1

    0
    -1
    3
    3 1
    3 2
    4 3

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

首页