B. Swaps- Codeforces Round #743 (Div. 2)

原文链接https://codeforces.com/problemset/problem/1573/B

制作不易,你的关注和点赞将为我的CF之路注入动力!

B. Swaps

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Example

input

Copy

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

output

Copy

0
2
3

Note

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

---------------------------------------------------------------------------------------------------------------------------------

首先,两个数组一组偶数,一组奇数,条件不是白给的,它意味着,没有一位上,两个数组元素相同。也就是说,第一个位置,必定会不相同,而不相同就直接导致了字典序一大一小。也就是说,第一个位置的大小关系就直接决定了字典序关系,不满足上小于下的时候,就必须调整位置,而且必须调整第一个位置,而且只必须把第一个位置调成小于偶数第一个位置就行。

另外,不难发现,只和相邻的位置交换,某个位置到达首位置消耗步数是i-1

我们不知道哪种搭配会出现最小值,所以我们枚举奇数首位,对于每个奇数首位,我们找比它大的偶数的步数,一个一个枚举偶数步数,必定会超时,这里运用到一些dp思想

对于偶数,从2N开始,cnt[i]=min(cnt[i],cnt[i+2])这样,cnt[i]就代表了包括i,和大于i的全部偶数移动到首位的最小步数,枚举奇数的时候,只需要把奇数步数加上cnt[i+1]即可

详见代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long int ll;

int cnt[100000*2+10];

int main()
{
    int t;
    cin>>t;

    while(t--)
    {
        int n;
        cin>>n;
       memset(cnt,0,sizeof(cnt));
        for(int i=1;i<=n;i++)
        {
            int x;
            cin>>x;
            cnt[x]=i-1;

        }
        for(int i=1;i<=n;i++)
        {
            int x;
            cin>>x;
            cnt[x]=i-1;

        }

        cnt[2*n+2]=99999999;

        for(int i=2*n;i>=2;i-=2)
        {
            cnt[i]=min(cnt[i],cnt[i+2]);
        }
        int ans=999999999;
        for(int i=1;i<=2*n;i+=2)
        {
            ans=min(ans,cnt[i]+cnt[i+1]);
        }
        cout<<ans<<'\n';
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值