CF584E.Anton and Ira

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Anton loves transforming one permutation into another one by swapping elements for money, and Ira doesn't like paying for stupid games. Help them obtain the required permutation by paying as little money as possible.

More formally, we have two permutations, p and s of numbers from 1 to n. We can swap p__i and p__j, by paying |i - j| coins for it. Find and print the smallest number of coins required to obtain permutation s from permutation p. Also print the sequence of swap operations at which we obtain a solution.

安东喜欢通过交换元素(并支付费用)将一个排列变为另一个排列,而伊拉不喜欢为无聊的游戏付费。请帮助他们以最少的费用来得到目标排列。

更准确地说,我们有两个由 11 到 nn 构成的排列 pp 和 ss。我们可以交换 pip_i 和 pjp_j,代价为 ∣i−j∣|i - j| 枚硬币。请找出并输出将排列 pp 变为排列 ss 所需的最少硬币数;同时输出达成该目标的一组交换操作序列。

输入格式

The first line contains a single number n (1 ≤ n ≤ 2000) — the length of the permutations.

The second line contains a sequence of n numbers from 1 to n — permutation p. Each number from 1 to n occurs exactly once in this line.

The third line contains a sequence of n numbers from 1 to n — permutation s. Each number from 1 to n occurs once in this line.

第一行包含一个整数 nn(1≤n≤20001 \leq n \leq 2000)——排列的长度。

第二行包含 nn 个从 11 到 nn 的整数——排列 pp。11 到 nn 中的每个数在此行中恰好出现一次。

第三行包含 nn 个从 11 到 nn 的整数——排列 ss。11 到 nn 中的每个数在此行中恰好出现一次。

输出格式

In the first line print the minimum number of coins that you need to spend to transform permutation p into permutation s.

In the second line print number k (0 ≤ k ≤ 2·106) — the number of operations needed to get the solution.

In the next k lines print the operations. Each line must contain two numbers i and j (1 ≤ i, j ≤ n, i ≠ j), which means that you need to swap p__i and p__j.

It is guaranteed that the solution exists.

第一行输出将排列 pp 变换为排列 ss 所需花费的最少硬币数。

第二行输出操作次数 kk(0 ≤ k ≤ 2⋅1060 ≤ k ≤ 2·10^6)。

接下来的 kk 行,每行输出两个数 ii 和 jj(1 ≤ i, j ≤ n1 ≤ i, j ≤ n,且 i ≠ ji ≠ j),表示需交换 pip_i 与 pjp_j。

保证解存在。

输入输出样例

  • 输入#1

    4
    4 2 1 3
    3 2 4 1

    输出#1

    3
    2
    4 3
    3 1

说明/提示

In the first sample test we swap numbers on positions 3 and 4 and permutation p becomes 4 2 3 1. We pay |3 - 4| = 1 coins for that. On second turn we swap numbers on positions 1 and 3 and get permutation 3241 equal to s. We pay |3 - 1| = 2 coins for that. In total we pay three coins.

在第一个样例测试中,我们交换位置 3 和 4 上的数字,排列 pp 变为 4 2 3 14\ 2\ 3\ 1,为此支付 ∣3 − 4∣ = 1|3 - 4| = 1 枚硬币。在第二步中,我们交换位置 1 和 3 上的数字,得到排列 3 2 4 13\ 2\ 4\ 1,其等于 ss,为此支付 ∣3 − 1∣ = 2|3 - 1| = 2 枚硬币。总共支付了三枚硬币。

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

首页