CF1573B.Swaps
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given two arrays a and b of length n. Array a contains each odd integer from 1 to 2n in an arbitrary order, and array b contains each even integer from 1 to 2n in an arbitrary order.
You can perform the following operation on those arrays:
- choose one of the two arrays
- pick an index i from 1 to n−1
- swap the i-th and the (i+1)-th elements of the chosen array
Compute the minimum number of operations needed to make array a lexicographically smaller than array b.
For two different arrays x and y of the same length n, we say that x is lexicographically smaller than y if in the first position where x and y differ, the array x has a smaller element than the corresponding element in y.
你有两个长度为 n 的数组 a 和 b。数组 a 包含从 1 到 2n 的所有奇数(共 n 个),以任意顺序排列;数组 b 包含从 1 到 2n 的所有偶数(共 n 个),也以任意顺序排列。
你可以对这两个数组执行以下操作:
- 选择其中一个数组;
- 选择一个下标 i,满足 1≤i≤n−1;
- 交换所选数组中第 i 个与第 (i+1) 个元素。
请计算使数组 a 在字典序上严格小于数组 b 所需的最少操作次数。
对于两个长度均为 n 的不同数组 x 和 y,若在 x 与 y 首次出现差异的位置上,x 中对应位置的元素值小于 y 中对应位置的元素值,则称 x 在字典序上小于 y。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104).
The first line of each test case contains a single integer n (1≤n≤105) — the length of the arrays.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤2n, all ai are odd and pairwise distinct) — array a.
The third line of each test case contains n integers b1,b2,…,bn (1≤bi≤2n, all bi are even and pairwise distinct) — array b.
It is guaranteed that the sum of n over all test cases does not exceed 105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。
每个测试用例的第一行包含一个整数 n(1≤n≤105)—— 数组的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤2n,所有 ai 均为奇数且两两不同)—— 数组 a。
每个测试用例的第三行包含 n 个整数 b1,b2,…,bn(1≤bi≤2n,所有 bi 均为偶数且两两不同)—— 数组 b。
保证所有测试用例的 n 之和不超过 105。
输出格式
For each test case, print one integer: the minimum number of operations needed to make array a lexicographically smaller than array b.
We can show that an answer always exists.
对于每个测试用例,输出一个整数:使数组 a 字典序小于数组 b 所需的最少操作次数。
可以证明答案一定存在。
输入输出样例
输入#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 a is already lexicographically smaller than array b, so no operations are required.
In the second example, we can swap 5 and 3 and then swap 2 and 4, which results in [3,5,1] and [4,2,6]. Another correct way is to swap 3 and 1 and then swap 5 and 1, which results in [1,5,3] and [2,4,6]. Yet another correct way is to swap 4 and 6 and then swap 2 and 6, which results in [5,3,1] and [6,2,4].
在第一个例子中,数组 a 已经字典序小于数组 b,因此无需执行任何操作。
在第二个例子中,我们可以先交换 5 和 3,再交换 2 和 4,得到 [3,5,1] 和 [4,2,6]。另一种正确的方法是先交换 3 和 1,再交换 5 和 1,得到 [1,5,3] 和 [2,4,6]。还有一种正确的方法是先交换 4 和 6,再交换 2 和 6,得到 [5,3,1] 和 [6,2,4]。
输入解题思路,AI测评打分。不知道怎么写?