CF1882E2.Two Permutations (Hard Version)

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This is the hard version of the problem. The difference between the two versions is that you 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 way to achieve the goal using the minimum number of operations possible, or say that none exists. Please note that you have to minimize the number of 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 开始重新编号。

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

请找出一种使用最少操作次数达成目标的方法,或判定不存在可行方案。请注意,你必须最小化操作次数。

†^{\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 — 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 have to minimize the number of operations.

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

否则,先输出一个整数 kk —— 表示需要执行的操作次数,随后输出 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

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

    2 2
    1 2
    2 1

    输出#3

    -1

说明/提示

In the first test case, 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 test case, 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测评打分。不知道怎么写?

首页