CF1573B.Swaps

普及/提高-

通过率:0%

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 n1n-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 .

输入格式

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

The first line of each test case contains a single integer nn ( 1n1051 \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 ( 1ai2n1 \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 ( 1bi2n1 \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 .

输出格式

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.

输入输出样例

  • 输入#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] .

首页