CF1882E1.Two Permutations (Easy Version)

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This is the easy version of the problem. The difference between the two versions is that you do not have to minimize the number of operations in this version. You can make hacks only if both versions of the problem are solved.

You have two permutations†^{\dagger} p1,p2,…,pnp_{1}, p_{2}, \ldots, p_{n} (of integers 11 to nn) and q1,q2,…,qmq_{1}, q_{2}, \ldots, q_{m} (of integers 11 to mm). Initially pi=aip_{i}=a_{i} for i=1,2,…,ni=1, 2, \ldots, n, and qj=bjq_{j} = b_{j} for j=1,2,…,mj = 1, 2, \ldots, m. You can apply the following operation on the permutations several (possibly, zero) times.

In one operation, pp and qq will change according to the following three steps:

  • You choose integers ii, jj which satisfy 1≤i≤n1 \le i \le n and 1≤j≤m1 \le j \le m.
  • Permutation pp is partitioned into three parts using pip_i as a pivot: the left part is formed by elements p1,p2,…,pi−1p_1, p_2, \ldots, p_{i-1} (this part may be empty), the middle part is the single element pip_i, and the right part is pi+1,pi+2,…,pnp_{i+1}, p_{i+2}, \ldots, p_n (this part may be empty). To proceed, swap the left and the right parts of this partition. Formally, after this step, pp will become pi+1,pi+2,…,pn,pi,p1,p2,…,pi−1p_{i+1}, p_{i+2}, \ldots, p_{n}, p_{i}, p_{1}, p_{2}, \ldots, p_{i-1}. The elements of the newly formed pp will be reindexed starting from 11.
  • Perform the same transformation on qq with index jj. Formally, after this step, qq will become qj+1,qj+2,…,qm,qj,q1,q2,…,qj−1q_{j+1}, q_{j+2}, \ldots, q_{m}, q_{j}, q_{1}, q_{2}, \ldots, q_{j-1}. The elements of the newly formed qq will be reindexed starting from 11.

Your goal is to simultaneously make pi=ip_{i}=i for i=1,2,…,ni=1, 2, \ldots, n, and qj=jq_{j} = j for j=1,2,…,mj = 1, 2, \ldots, m.

Find any valid way to achieve the goal using at most 10 00010\,000 operations, or say that none exists. Please note that you do not have to minimize the number of operations.

It can be proved that if it is possible to achieve the goal, then there exists a way to do so using at most 10 00010\,000 operations.

†^{\dagger} A permutation of length kk is an array consisting of kk distinct integers from 11 to kk in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (k=3k=3 but there is 44 in the array).

这是该问题的简单版本。两个版本的区别在于:在此版本中,你无需最小化操作次数。仅当两个版本的问题均被解决时,你才可以进行 Hack。

你有两个排列†^{\dagger}:p1,p2,…,pnp_{1}, p_{2}, \ldots, p_{n}(由整数 11 到 nn 构成)和 q1,q2,…,qmq_{1}, q_{2}, \ldots, q_{m}(由整数 11 到 mm 构成)。初始时,对 i=1,2,…,ni=1, 2, \ldots, n,有 pi=aip_{i}=a_{i};对 j=1,2,…,mj = 1, 2, \ldots, m,有 qj=bjq_{j} = b_{j}。你可以对这两个排列执行如下操作若干次(也可以不执行)。

每次操作包含以下三个步骤:

  • 选择满足 1≤i≤n1 \le i \le n 和 1≤j≤m1 \le j \le m 的整数 ii、jj;
  • 将排列 pp 以 pip_i 为轴点划分为三部分:左部由元素 p1,p2,…,pi−1p_1, p_2, \ldots, p_{i-1} 构成(该部分可能为空),中部为单个元素 pip_i,右部为 pi+1,pi+2,…,pnp_{i+1}, p_{i+2}, \ldots, p_n(该部分可能为空)。接下来,交换该划分中的左部与右部。形式化地,此步之后,pp 变为 pi+1,pi+2,…,pn,pi,p1,p2,…,pi−1p_{i+1}, p_{i+2}, \ldots, p_{n}, p_{i}, p_{1}, p_{2}, \ldots, p_{i-1}。新得到的 pp 的元素将从下标 11 开始重新编号;
  • 对 qq 在索引 jj 处执行相同的变换。形式化地,此步之后,qq 变为 qj+1,qj+2,…,qm,qj,q1,q2,…,qj−1q_{j+1}, q_{j+2}, \ldots, q_{m}, q_{j}, q_{1}, q_{2}, \ldots, q_{j-1}。新得到的 qq 的元素将从下标 11 开始重新编号。

你的目标是同时使得对所有 i=1,2,…,ni=1, 2, \ldots, n 满足 pi=ip_{i}=i,且对所有 j=1,2,…,mj = 1, 2, \ldots, m 满足 qj=jq_{j} = j。

请找出任意一种在至多 10 00010\,000 次操作内达成目标的方法,或判定不存在这样的方法。请注意,你无需最小化操作次数。

可以证明:若目标可达,则必存在一种至多使用 10 00010\,000 次操作的方案。

†^{\dagger} 长度为 kk 的排列是指由 11 到 kk 中 kk 个互异整数按任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数字 22 在数组中出现两次),[1,3,4][1,3,4] 也不是排列(此时 k=3k=3,但数组中出现了 44)。

输入格式

The first line contains two integers nn and mm (1≤n,m≤25001 \le n, m \le 2500).

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

The third line contains mm integers b1,b2,…,bmb_1, b_2, \ldots, b_m (1≤bi≤m1 \le b_i \le m).

It is guaranteed that aa and bb are permutations.

第一行包含两个整数 nn 和 mm(1≤n,m≤25001 \le n, m \le 2500)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \le a_i \le n)。

第三行包含 mm 个整数 b1,b2,…,bmb_1, b_2, \ldots, b_m(1≤bi≤m1 \le b_i \le m)。

保证 aa 和 bb 均为排列。

输出格式

If there is no solution, print a single integer −1-1.

Otherwise, print an integer kk (0≤k≤10 0000 \le k \le 10\,000) — the number of operations to perform, followed by kk lines, each containing two integers ii and jj (1≤i≤n1 \le i \le n, 1≤j≤m1 \le j \le m) — the integers chosen for the operation.

If there are multiple solutions, print any of them.

Please note that you do not have to minimize the number of operations.

如果无解,请输出单个整数 −1-1。

否则,先输出一个整数 kk(0≤k≤10 0000 \le k \le 10\,000)—— 表示需执行的操作次数,随后输出 kk 行,每行包含两个整数 ii 和 jj(1≤i≤n1 \le i \le n,1≤j≤m1 \le j \le m)—— 表示该次操作所选的下标。

若存在多个解,输出任意一个即可。

请注意,你无需最小化操作次数。

输入输出样例

  • 输入#1

    3 5
    2 1 3
    5 2 1 4 3

    输出#1

    2
    3 4
    2 4
  • 输入#2

    4 4
    3 4 2 1
    2 4 1 3

    输出#2

    5
    4 2
    3 3
    1 4
    3 2
    4 1
  • 输入#3

    2 2
    1 2
    2 1

    输出#3

    -1

说明/提示

In the first example, we can achieve the goal within 22 operations:

  1. In the first operation, choose i=3i = 3, j=4j = 4. After this, pp becomes [3,2,1][3, 2, 1] and qq becomes [3,4,5,2,1][3, 4, 5, 2, 1].
  2. In the second operation, choose i=2i = 2, j=4j = 4. After this, pp becomes [1,2,3][1, 2, 3] and qq becomes [1,2,3,4,5][1, 2, 3, 4, 5].

In the third example, it is impossible to achieve the goal.

在第一个例子中,我们可以在 22 次操作内达成目标:

  1. 第一次操作中,选择 i=3i = 3、j=4j = 4。操作后,pp 变为 [3,2,1][3, 2, 1],qq 变为 [3,4,5,2,1][3, 4, 5, 2, 1]。
  2. 第二次操作中,选择 i=2i = 2、j=4j = 4。操作后,pp 变为 [1,2,3][1, 2, 3],qq 变为 [1,2,3,4,5][1, 2, 3, 4, 5]。

在第三个例子中,无法达成目标。

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

首页