CF1918B.Minimize Inversions

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given two permutations aa and bb of length nn. A permutation is an array of nn elements from 11 to nn where all elements are distinct. For example, an array [2,1,32,1,3] is a permutation, but [0,10,1] and [1,3,11,3,1] aren't.

You can (as many times as you want) choose two indices ii and jj, then swap aia_i with aja_j and bib_i with bjb_j simultaneously.

You hate inversions, so you want to minimize the total number of inversions in both permutations.

An inversion in a permutation pp is a pair of indices (i,j)(i, j) such that i<ji \lt j and pi>pjp_i \gt p_j. For example, if p=[3,1,4,2,5]p=[3,1,4,2,5] then there are 33 inversions in it (the pairs of indices are (1,2)(1,2), (1,4)(1,4) and (3,4)(3,4)).

给你两个长度为 nn 的排列 aa 和 bb。排列是指由 11 到 nn 中的 nn 个互不相同的数组成的数组。例如,数组 [2,1,3][2,1,3] 是一个排列,但 [0,1][0,1] 和 [1,3,1][1,3,1] 不是。

你可以(任意多次)选择两个下标 ii 和 jj,然后同时交换 aia_i 与 aja_j,以及 bib_i 与 bjb_j。

你讨厌逆序对,因此希望最小化两个排列中逆序对的总数。

在排列 pp 中,一个逆序对是指一对下标 (i,j)(i, j),满足 i<ji \lt j 且 pi>pjp_i \gt p_j。例如,若 p=[3,1,4,2,5]p=[3,1,4,2,5],则其中包含 33 个逆序对(对应下标对为 (1,2)(1,2)、(1,4)(1,4) 和 (3,4)(3,4))。

输入格式

The first line contains an integer tt (1≤t≤20 0001 \leq t \leq 20\,000) — the number of test cases.

Each test case consists of three lines. The first line contains an integer nn (1≤n≤2⋅1051 \leq n \leq 2\cdot10^5) — the length of the permutations aa and bb. The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \leq a_i \leq n) — permutation aa. The third line contains bb in a similar format.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot10^5.

第一行包含一个整数 tt(1≤t≤20 0001 \leq t \leq 20\,000)—— 测试用例的数量。

每个测试用例由三行组成。第一行包含一个整数 nn(1≤n≤2⋅1051 \leq n \leq 2\cdot10^5)—— 排列 aa 和 bb 的长度。第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \leq a_i \leq n)—— 排列 aa。第三行以类似格式给出排列 bb。

保证所有测试用例的 nn 之和不超过 2⋅1052\cdot10^5。

输出格式

For each test case, output two permutations a′a' and b′b' (in the same format as in the input) — the permutations after all operations. The total number of inversions in a′a' and b′b' should be the minimum possible among all pairs of permutations that can be obtained using operations from the statement.

If there are multiple solutions, print any of them.

对于每个测试用例,输出两个排列 a′a' 和 b′b'(格式与输入中相同)——即执行完所有操作后的排列。a′a' 和 b′b' 中的逆序对总数应在所有可通过题目所述操作得到的排列对中达到最小值。

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

输入输出样例

  • 输入#1

    3
    5
    1 2 3 4 5
    5 4 3 2 1
    3
    3 1 2
    3 1 2
    6
    2 5 6 1 3 4
    1 5 3 6 2 4

    输出#1

    3 2 5 1 4
    3 4 1 5 2
    1 2 3
    1 2 3
    2 3 4 6 5 1
    1 2 4 3 5 6

说明/提示

In the first test case, the minimum possible number of inversions is 1010.

In the second test case, we can sort both permutations at the same time. For this, the following operations can be done:

  • Swap the elements in the positions 11 and 33 in both permutations. After the operation, a=a = [2,1,32,1,3], b=b = [2,1,32,1,3].
  • Swap the elements in the positions 11 and 22. After the operations, aa and bb are sorted.

In the third test case, the minimum possible number of inversions is 77.

在第一个测试用例中,最少可能的逆序对数量为 1010。

在第二个测试用例中,我们可以同时将两个排列排序。为此,可执行以下操作:

  • 同时在两个排列中交换位置 11 和 33 上的元素。操作后,a=[2,1,3]a = [2,1,3],b=[2,1,3]b = [2,1,3]。
  • 交换位置 11 和 22 上的元素。操作后,aa 和 bb 均已排序。

在第三个测试用例中,最少可能的逆序对数量为 77。

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

首页