Swaps

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.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104).

The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105) — the length of the arrays.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤2n1≤ai≤2n, all aiai are odd and pairwise distinct) — array aa.

The third line of each test case contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤2n1≤bi≤2n, all bibi are even and pairwise distinct) — array bb.

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

Output

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.

Input:

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

 

Out:

0
2
3

        思路:因为要保证b序列大于a序列,而a,b序列一定是不相同的,即要么a大于b,要么反之。因此,要保证第一个数字就要让b大于a,问题就转化为最少的操作数使得让符合题意的数到第一个位置的操作数。

        然后以b为基准,在a中找最近的点,什么样的点满足题意呢?

        比如 :a = 1的位置在2,a = 3的位置在5。如果b = 4,那我们只要找比 <= 3的奇数的位置。那像a = 3的位置有意义吗?是没有意义的,因为有一个比他更小的数在前面,这时候只要让储存a的位置的数组转换一下即可,就比如a = 3的位置数组,就可以 = 1了。

        完整代码:

#include
#define int long long 
#define rep(i, a, b) for(int i = a; i <= b; ++ i)
#define per(i, a, b) for(int i = a; i >= b; -- i)
using namespace std;
const int N = 1e5 + 10,mod = 1e9 + 7;
 
int cnt[N];

void solve(){
	int n; scanf("%lld",&n);
	int ans = 1e9;
	rep(i,1,n) {
		int a; cin >> a;
		cnt[a+1 >> 1] = i;
	}
	rep(i,2,n) cnt[i] = min(cnt[i],cnt[i-1]);
	rep(i,1,n){
		int b; cin >> b;
		ans = min(ans,cnt[b>>1]+i-2); 
	}
	cout << ans << endl;
}
signed main(){
    int t=1; cin>>t;
    while(t--) solve();
    return 0;
}

你可能感兴趣的:(codeforces补题,c++,算法,acm竞赛,蓝桥杯)