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;
  }
}





相关文章
|
云安全 运维 负载均衡
【纯干货】针对《等保2.0》要求的云上最佳实践——网络安全篇
伴随着国内企业上云步伐的加快,越来越多的企业需要对云上关键业务进行等级保护自查或完成相关认证。本文以《GB/T 22239-2019 信息安全技术 网络安全等级保护基本要求》中所要求的三级标准为参考,重点关注其中所涉及的网络安全高危风险部分,为企业提供阿里云上有针对性的安全建设最佳实践,助力企业构建层次化的云上网络安全防御体系,保障核心业务的安全运行。
3840 1
【纯干货】针对《等保2.0》要求的云上最佳实践——网络安全篇
|
3月前
|
API
阿里云千问办公企业版收费价格:标准版价格198元/月/席、旗舰版298元,可另外购买积分包
阿里云千问办公企业版分标准版(198元/月/席)与旗舰版(298元/月/席,100席起购),分别提供基础模型、管理后台及VPC部署、API管控等能力;支持按需增购积分包。详情请见官网。
|
4月前
|
缓存 编解码 并行计算
模型能装进显存,训练为什么还是 OOM?把 PyTorch 显存拆成 5 笔账
模型权重能够装进显存,不代表训练就能正常运行。本文拆解 PyTorch 训练中的参数、梯度、优化器状态、激活值和临时工作区,并给出显存监测代码及 OOM 排查顺序,覆盖梯度累积、混合精度、Gradient Checkpointing 和 LoRA 等常用方案。
|
10月前
|
数据处理 数据格式
在Excel中一次性粘贴多列数据时选择性粘贴特定格式
在Excel中一次性粘贴多列数据时选择性粘贴特定格式
1285 2
|
4月前
|
人工智能 缓存 移动开发
浏览器本地运行中文 AI 配音:Hojo TTS Light 80M 的 WebGPU、WASM 与模型切片实践
Timeline Studio 将 Hojo TTS Light 80M 集成至浏览器,实现纯前端中英混合配音:基于 WebGPU 自回归推理 + WASM 波形解码,支持 ONNX 模型分片下载、双镜像(ModelScope/HF)自动回退、智能分句与音频优先时间线同步,全程无需上传隐私数据。
|
7月前
|
JSON 前端开发 程序员
初级程序员必备的十大技能之 API 接口与前后端联调(三)
教程来源 https://wkmsa.cn/ 本节详解前后端联调完整流程:涵盖接口设计约定(含规范文档示例)、Mock数据并行开发(代码/JSON Server/Mock.js三方式)、前端API封装最佳实践(拦截器、模块化、组件调用),以及主流请求库对比与Axios封装示例,助力高效协同开发。
|
8月前
|
人工智能 运维 自然语言处理
高合规低成本双达标:PandaWiki+BeeParser 成为企业私有知识库最优解
企业数字化转型中,技术标准沉淀、知识安全管控成为制造业技术团队核心刚需。传统文档管理普遍存在数据孤岛、检索低效、存量资料整理成本高等痛点。
727 0
|
消息中间件 存储 Java
RocketMQ延时消息的原理与实现
本文分享了RocketMQ的延时消息的原理和实现,手把手带你从源码角度了解到内部实现机制。
1438 13
RocketMQ延时消息的原理与实现
|
数据可视化 Java 文件存储
Rational Rose 2007简单介绍及详细安装步骤
Rational Rose 2007简单介绍及详细安装步骤
2746 1
Rational Rose 2007简单介绍及详细安装步骤
|
数据采集 JSON 资源调度
Pont - 搭建前后端之桥
pont 在法语中是“桥”的意思,寓意着前后端之间的桥梁。
Pont - 搭建前后端之桥