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





相关文章
|
5月前
|
数据采集 机器学习/深度学习 人工智能
大模型“驯化”指南:从人类偏好到专属AI,PPO与DPO谁是你的菜?
本文深入解析让AI“懂你”的关键技术——偏好对齐,对比PPO与DPO两种核心方法。PPO通过奖励模型间接优化,适合复杂场景;DPO则以对比学习直接训练,高效稳定,更适合大多数NLP任务。文章涵盖原理、实战步骤、评估方法及选型建议,并推荐从DPO入手、结合低代码平台快速验证。强调数据质量与迭代实践,助力开发者高效驯化大模型,实现个性化输出。
875 8
|
5月前
|
人工智能 架构师 安全
后大模型时代:从 Model 到 Agent,下一代企业级应用架构演进指南
摘要:当大模型(LLM)的能力逐渐同质化,基于 Agent(智能体)的应用架构正在成为新的技术护城河。本文将探讨 Agent 如何重构软件开发范式,并梳理开发者在这一转型过程中所需的关键技术栈。
572 2
|
6月前
|
消息中间件 自然语言处理 供应链
Pandabuy复制指南:淘宝1688代购系统搭建
Pandabuy以“反向海淘”模式为核心,助力海外用户代购中国商品,主打欧美市场。通过物流收费、佣金、汇率差等多元盈利,两年营收达40亿元。依托网红营销、低价策略与社区运营,实现高速增长。系统搭建推荐微服务架构,对接淘宝1688 API,强化合规与供应链管理,结合SEO、社媒营销及风控体系,打造高效安全的国际代购平台。
|
12月前
|
数据采集 Web App开发 数据可视化
Python爬取豆瓣短评并生成词云分析
Python爬取豆瓣短评并生成词云分析
|
存储 安全 大数据
阿里云存储:优缺点深度剖析
阿里云存储是国内领先的云存储服务,具备高效稳定、弹性可扩展、安全可靠及丰富的产品线等优点,适用于各种规模的企业。其分布式架构支持高并发和大数据处理,提供多层次的安全防护和灵活的存储方案。然而,成本较高、数据安全风险和网络连接稳定性等问题也需关注。用户应根据需求权衡利弊,选择合适的存储方案。
1337 74
|
12月前
|
机器学习/深度学习 人工智能 算法
检索增强预训练框架MaskSearch:让AI更聪明地“找答案”
通义实验室发布的MaskSearch预训练框架,让AI具备“主动搜索+多步推理”能力,更精准回答复杂问题。其核心是检索增强掩码预测(RAMP)任务,通过监督微调与强化学习结合,使小模型性能媲美大模型。实验表明,在多个问答数据集中,经过RAMP预训练的模型表现优异,尤其在多跳推理任务中优势显著。此外,课程学习策略帮助模型从简单到复杂逐步提升推理能力。相关论文与代码已开源。
293 0
|
人工智能 自然语言处理 机器人
在钉钉中与百炼DeepSeek大模型对话
DeepSeek发布了两款先进AI模型V3和R1,分别适用于对话AI、内容生成及推理任务。阿里云百炼提供DeepSeek模型API,通过AppFlow可在钉钉中快速配置与DeepSeek的对话功能,无需代码开发,10分钟内完成全部设置。用户需在钉钉开放平台创建应用、配置消息卡片、授权权限,并使用计算巢AppFlow创建连接流,最后配置钉钉机器人并发布应用版本,即可实现与DeepSeek大模型的交互。
1357 8
在钉钉中与百炼DeepSeek大模型对话
|
人工智能 算法 数据挖掘
AAAI 2025| S5VH: 基于选择性状态空间的高效自监督视频哈希
随着短视频、流媒体平台的爆发式增长,如何高效地索引和检索视频数据成为计算机视觉和多媒体领域的重要研究问题。视频哈希(Video Hashing) 是一种通过学习紧凑的二进制编码来高效索引和检索视频的技术,其核心目标是使哈希码的汉明距离(Hamming Distance)能够准确地反映视频之间的语义相似性。
351 18
|
人工智能 自然语言处理 运维
新员工培训全攻略:从战略解码到实战落地的深度拆解
当航天科工七〇六所通过InfoQ的“线上+线下混合式培训”将200名新员工的岗位胜任周期缩短40%,当某芯片巨头用“铸造成长·一苇可航”主题培训将企业文化转化率达78%,我们不得不思考:在AI重构生产关系的今天,如何让培训计划既承载战略意图,又点燃个体价值?