B. Swaps<743,div2>

简介: B. Swaps<743,div2>

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


#include <bits/stdc++.h>
using namespace std;
int a[1000005], b[1000005];
int main() {
  int t;
  cin >> t;
  while (t--) {
    map<int, int>mp;
    int mixn = 0, minxx = 100000007, minxf = 100000007;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      mp[a[i]] = i;
    }
    sort(a, a + n);
    for (int i = 0; i < n; i++) {
      cin >> b[i];
      mp[b[i]] = i;
    }
    sort(b, b + n);
    for (int i = 0; i < n; i++) {
      if (mp[a[i]] < minxx) {
        minxx = mp[a[i]];
      }
      mixn = mp[b[i]] + minxx;
      if (mixn < minxf) {
        minxf = mixn;
      }
    }
    cout << minxf << endl;
  }
}





相关文章
|
9月前
|
人工智能 自然语言处理
稀疏之美:MoE模型如何开启AI效率革命
稀疏之美:MoE模型如何开启AI效率革命
405 140
|
9月前
|
人工智能 架构师 安全
后大模型时代:从 Model 到 Agent,下一代企业级应用架构演进指南
摘要:当大模型(LLM)的能力逐渐同质化,基于 Agent(智能体)的应用架构正在成为新的技术护城河。本文将探讨 Agent 如何重构软件开发范式,并梳理开发者在这一转型过程中所需的关键技术栈。
906 2
|
9月前
|
JSON 安全 JavaScript
小模型大作为:小型语言模型如何在钓鱼网站攻防战中“以轻制重”?
小型语言模型(SLM)正悄然变革网络安全防线。2025年研究证实,参数量10亿至200亿的SLM可在本地高效识别钓鱼网站,准确率高达89%,无需联网、保护隐私,为中国等重视数据主权的国家提供自主可控的新路径。
435 7
|
10月前
|
消息中间件 自然语言处理 供应链
Pandabuy复制指南:淘宝1688代购系统搭建
Pandabuy以“反向海淘”模式为核心,助力海外用户代购中国商品,主打欧美市场。通过物流收费、佣金、汇率差等多元盈利,两年营收达40亿元。依托网红营销、低价策略与社区运营,实现高速增长。系统搭建推荐微服务架构,对接淘宝1688 API,强化合规与供应链管理,结合SEO、社媒营销及风控体系,打造高效安全的国际代购平台。
|
11月前
|
存储
服务器数据恢复—服务器raid硬盘盘片划伤的数据恢复案例
服务器存储中有一组raid。该组raid中的两块硬盘出现故障:一块硬盘已经开盘,盘片损伤严重;另一块硬盘虽然没有开盘,但是可以初步判断其盘片也存在损伤。需要恢复该组raid数据。
|
10月前
|
存储 XML Rust
高效安全的数据序列化:Rust bincode二进制编码库入门指南(手把手教你使用bincode进行Rust二进制序列化)
本教程来源https://www.vpshk.cn/带你快速掌握Rust中bincode库的使用,实现高效、安全的二进制序列化与反序列化,适用于高性能服务、游戏引擎等场景,助力提升数据处理效率。
|
人工智能 JSON Java
【极速入门版】编程小白也能轻松上手Comate AI编程插件
【极速入门版】编程小白也能轻松上手Comate AI编程插件
597 0
|
存储 安全 大数据
阿里云存储:优缺点深度剖析
阿里云存储是国内领先的云存储服务,具备高效稳定、弹性可扩展、安全可靠及丰富的产品线等优点,适用于各种规模的企业。其分布式架构支持高并发和大数据处理,提供多层次的安全防护和灵活的存储方案。然而,成本较高、数据安全风险和网络连接稳定性等问题也需关注。用户应根据需求权衡利弊,选择合适的存储方案。
1474 74
|
数据采集 Web App开发 数据可视化
Python爬取豆瓣短评并生成词云分析
Python爬取豆瓣短评并生成词云分析
|
Java
在Java中如何实现接口?
实现接口是 Java 编程中的一个重要环节,它有助于提高代码的规范性、可扩展性和复用性。通过正确地实现接口,可以使代码更加灵活、易于维护和扩展。
993 64

热门文章

最新文章