AtCoder Regular Contest 214
E
交换 K 次
Swap K times
AtCoder
⌀ 2651
原题
← DF →

交换 K 次AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

You are given integer sequences of length $N$, $A = (A_1, \dots ,A_N)$ and $B = (B_1, \dots ,B_N)$, and a positive integer $K$.

给定长度为 $N$ 的整数序列 $A = (A_1, \dots ,A_N)$ 和 $B = (B_1, \dots ,B_N)$,以及一个正整数 $K$。AI (CI翻译)

Each time you pay a cost of $1$, you can perform adjacent swaps exactly $K$ times. An adjacent swap refers to the following operation:

每支付 $1$ 的代价,你可以恰好进行 $K$ 次相邻交换。相邻交换指以下操作:AI (CI翻译)

  • Choose an integer $i$ between $1$ and $N-1$, inclusive, and swap $A_i$ and $A_{i+1}$.
  • 选择一个介于 $1$ 和 $N-1$(含)之间的整数 $i$,交换 $A_i$ 和 $A_{i+1}$。
AI (CI翻译)

Determine if it is possible to make $A$ match $B$. If possible, find the minimum cost required to make them match.

判断能否使 $A$ 与 $B$ 相同。如果可以,求使它们相同所需的最小代价。AI (CI翻译)

You are given $T$ test cases; find the answer for each of them.

给定 $T$ 组测试用例,请分别求出每组的答案。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1 \le T \le 10^5$
  • $2 \le N \le 3 \times 10^5$
  • $1 \le K \le 10^9$
  • $1 \le A_i,B_i \le N$
  • $\{A_1,\dots ,A_N\}$ and $\{B_1,\dots ,B_N\}$ match as multisets.
  • The sum of $N$ over all test cases is at most $3 \times 10^5$.
  • All input values are integers.
  • $1 \le T \le 10^5$
  • $2 \le N \le 3 \times 10^5$
  • $1 \le K \le 10^9$
  • $1 \le A_i,B_i \le N$
  • ${A_1,\dots ,A_N}$ 和 ${B_1,\dots ,B_N}$ 作为多重集相同。
  • 所有测试用例的 $N$ 之和不超过 $3 \times 10^5$。
  • 所有输入值均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

The input is given from Standard Input in the following format:

输入从标准输入读取,格式如下:AI (CI翻译)

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
AI (CI翻译)

Each test case is given in the following format:

每组测试用例的格式如下:AI (CI翻译)

N K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

N K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Output $T$ lines.

输出 $T$ 行。AI (CI翻译)

The $i$-th line should contain the minimum cost if it is possible to make $A$ match $B$ for the $i$-th test case, and -1 otherwise.

第 $i$ 行应包含使第 $i$ 组测试用例中的 $A$ 与 $B$ 相同所需的最小代价;如果无法做到,则输出 -1。AI (CI翻译)

样例

Input #1
3
5 4
3 1 4 1 5
5 1 3 1 4
2 1000000000
1 2
2 1
10 13
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
Output #1
2
-1
5
For the first test case, it is possible to make $A$ and $B$ match with a cost of $2$ by performing the following operations: 1. Pay a cost of $1$, and choose $i$ as $3,1,4,1$ in order for each adjacent swap, making $A = (3,1,1,5,4)$. 2. Pay a cost of $1$, and choose $i$ as $3,1,2,1$ in order for each adjacent swap, making $A = (5,1,3,1,4)$. For the second test case, it is impossible to make $A$ and $B$ match regardless of the cost.
题目信息
题号
arc214_e
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。