CF1573B.Swaps

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given two arrays aa and bb of length nn. Array aa contains each odd integer from 11 to 2n2n in an arbitrary order, and array bb contains each even integer from 11 to 2n2n in an arbitrary order.

You can perform the following operation on those arrays:

  • choose one of the two arrays
  • pick an index ii from 11 to n−1n-1
  • swap the ii-th and the (i+1)(i+1)-th elements of the chosen array

Compute the minimum number of operations needed to make array aa lexicographically smaller than array bb.

For two different arrays xx and yy of the same length nn, we say that xx is lexicographically smaller than yy if in the first position where xx and yy differ, the array xx has a smaller element than the corresponding element in yy.

你有两个长度为 nn 的数组 aa 和 bb。数组 aa 包含从 11 到 2n2n 的所有奇数(共 nn 个),以任意顺序排列;数组 bb 包含从 11 到 2n2n 的所有偶数(共 nn 个),也以任意顺序排列。

你可以对这两个数组执行以下操作:

  • 选择其中一个数组;
  • 选择一个下标 ii,满足 1≤i≤n−11 \le i \le n-1;
  • 交换所选数组中第 ii 个与第 (i+1)(i+1) 个元素。

请计算使数组 aa 在字典序上严格小于数组 bb 所需的最少操作次数。

对于两个长度均为 nn 的不同数组 xx 和 yy,若在 xx 与 yy 首次出现差异的位置上,xx 中对应位置的元素值小于 yy 中对应位置的元素值,则称 xx 在字典序上小于 yy。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4).

The first line of each test case contains a single integer nn (1≤n≤1051 \le n \le 10^5) — the length of the arrays.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤2n1 \le a_i \le 2n, all aia_i are odd and pairwise distinct) — array aa.

The third line of each test case contains nn integers b1,b2,…,bnb_1, b_2, \ldots, b_n (1≤bi≤2n1 \le b_i \le 2n, all bib_i are even and pairwise distinct) — array bb.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5)—— 数组的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤2n1 \le a_i \le 2n,所有 aia_i 均为奇数且两两不同)—— 数组 aa。

每个测试用例的第三行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \ldots, b_n(1≤bi≤2n1 \le b_i \le 2n,所有 bib_i 均为偶数且两两不同)—— 数组 bb。

保证所有测试用例的 nn 之和不超过 10510^5。

输出格式

For each test case, print one integer: the minimum number of operations needed to make array aa lexicographically smaller than array bb.

We can show that an answer always exists.

对于每个测试用例,输出一个整数:使数组 aa 字典序小于数组 bb 所需的最少操作次数。

可以证明答案一定存在。

输入输出样例

  • 输入#1

    3
    2
    3 1
    4 2
    3
    5 3 1
    2 4 6
    5
    7 5 9 1 3
    2 4 6 10 8

    输出#1

    0
    2
    3

说明/提示

In the first example, the array aa is already lexicographically smaller than array bb, so no operations are required.

In the second example, we can swap 55 and 33 and then swap 22 and 44, which results in [3,5,1][3, 5, 1] and [4,2,6][4, 2, 6]. Another correct way is to swap 33 and 11 and then swap 55 and 11, which results in [1,5,3][1, 5, 3] and [2,4,6][2, 4, 6]. Yet another correct way is to swap 44 and 66 and then swap 22 and 66, which results in [5,3,1][5, 3, 1] and [6,2,4][6, 2, 4].

在第一个例子中,数组 aa 已经字典序小于数组 bb,因此无需执行任何操作。

在第二个例子中,我们可以先交换 55 和 33,再交换 22 和 44,得到 [3,5,1][3, 5, 1] 和 [4,2,6][4, 2, 6]。另一种正确的方法是先交换 33 和 11,再交换 55 和 11,得到 [1,5,3][1, 5, 3] 和 [2,4,6][2, 4, 6]。还有一种正确的方法是先交换 44 和 66,再交换 22 和 66,得到 [5,3,1][5, 3, 1] 和 [6,2,4][6, 2, 4]。

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

首页