惹恼幽灵AI (CI翻译)
Ja the Ghost is playing with rubber ducks. He has $n$ piles of rubber ducks arranged in a row, where the $i$-th pile contains $a_i$ ducks. Quack the Duck gives Ja a strictly increasing sequence $b_1,b_2,\ldots,b_n$ and commands him to make the piles $a_1,a_2,\ldots,a_n$ become exactly this sequence.
Ja 幽灵正在玩橡皮鸭。他有 $n$ 堆排成一行的橡皮鸭,其中第 $i$ 堆有 $a_i$ 只鸭子。鸭子 Quack 给了 Ja 一个严格递增的序列 $b_1,b_2,\ldots,b_n$,并命令他将各堆鸭子的数量 $a_1,a_2,\ldots,a_n$ 变为恰好这个序列。AI (CI翻译)
Ja performs the process in the following two stages:
Ja 按以下两个阶段进行操作:AI (CI翻译)
- Ja may add any number of ducks to each pile.Formally, for each pile
$i$, he chooses a non-negative integer$x_i$and replaces$a_i$with$a_i+x_i$. - Ja may repeatedly swap two adjacent piles.Formally, he may perform the following operation any number of times, possibly zero: choose an index
$i$such that$1\le i\le n-1$, and swap the values of$a_i$and$a_{i+1}$.
- Ja 可以向每堆中添加任意数量的鸭子。形式化地,对于每一堆
$i$,他选择一个非负整数$x_i$,并将$a_i$替换为$a_i+x_i$。 - Ja 可以反复交换相邻的两堆。形式化地,他可以进行任意次(也可以一次都不进行)以下操作:选择一个满足
$1\le i\le n-1$的下标$i$,交换$a_i$和$a_{i+1}$的值。
A process is called valid if, after both stages end, the sequence of pile sizes is exactly $b_1,b_2,\ldots,b_n$.
如果两个阶段结束后,各堆的大小恰好为 $b_1,b_2,\ldots,b_n$,则称这一过程是有效的。AI (CI翻译)
Find the minimum possible number of operations performed in the second stage among all valid processes. If there is no valid process, output $-1$.
请求出所有有效的过程中,第二阶段所执行的操作次数的最小值。如果不存在有效的过程,输出 $-1$。AI (CI翻译)
Input
输入
AI (CI翻译)Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2000$). The description of the test cases follows.
每个测试包含多个测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 2000$)。接下来是各个测试用例的描述。AI (CI翻译)
The first line of each test case contains an integer $n$ ($1\le n\le 2000$) — the number of piles of rubber ducks.
每个测试用例的第一行包含一个整数 $n$($1\le n\le 2000$)——橡皮鸭堆的数量。AI (CI翻译)
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1\le a_i\le 10^9$) — the initial number of rubber ducks in each pile.
第二行包含 $n$ 个整数 $a_1, a_2, \ldots, a_n$($1\le a_i\le 10^9$)——每堆橡皮鸭的初始数量。AI (CI翻译)
The third line contains $n$ integers $b_1,b_2,\ldots,b_n$ ($1\le b_1 \lt b_2 \lt \cdots \lt b_n\le 10^9$) — the final number of rubber ducks in each pile.
第三行包含 $n$ 个整数 $b_1,b_2,\ldots,b_n$($1\le b_1 \lt b_2 \lt \cdots \lt b_n\le 10^9$)——每堆橡皮鸭的最终数量。AI (CI翻译)
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$.
保证所有测试用例中 $n$ 的总和不超过 $2000$。AI (CI翻译)
Output
输出
AI (CI翻译)For each test case, output a single integer — the minimum possible number of operations performed in the second stage among all valid processes. If there is no valid process, output $-1$.
对于每个测试用例,输出一个整数——所有有效的过程中,第二阶段所执行的操作次数的最小值。如果不存在有效的过程,输出 $-1$。AI (CI翻译)
Note
说明
AI (CI翻译)In the first test case, Ja only needs the first stage. He can set $x_1=0,x_2=1,x_3=3$, so the piles become $1,3,5$. No swaps are needed, so the answer is $0$.
在第一个测试用例中,Ja 只需要执行第一阶段。他可以令 $x_1=0,x_2=1,x_3=3$,这样各堆就变为 $1,3,5$。不需要交换,因此答案为 $0$。AI (CI翻译)
In the second test case, Ja needs both stages. He can set $x_1=0,x_2=1,x_3=0$, so the piles become $2,3,1$. Then he can perform two swaps: $$ [2,3,1]\to [2,1,3]\to [1,2,3]. $$ The pile with $1$ duck must move from the third position to the first position, so at least two swaps are necessary. Therefore the answer is $2$.
在第二个测试用例中,Ja 需要执行两个阶段。他可以令 $x_1=0,x_2=1,x_3=0$,这样各堆变为 $2,3,1$。然后他可以进行两次交换:$$ [2,3,1]\to [2,1,3]\to [1,2,3]. $$ 含有 $1$ 只鸭子的那堆必须从第三个位置移动到第一个位置,因此至少需要进行两次交换。所以答案为 $2$。AI (CI翻译)
In the third test case, it is impossible. The first pile initially contains $5$ ducks, but every number in the target sequence is at most $4$. Since Ja can only add ducks and cannot remove them, this pile cannot become equal to any number in the target sequence. Therefore the answer is $-1$.
在第三个测试用例中,这是不可能的。第一堆一开始就有 $5$ 只鸭子,但目标序列中的每个数都不超过 $4$。由于 Ja 只能添加鸭子而不能移除鸭子,因此这堆无法变为目标序列中的任何一个数。所以答案为 $-1$。AI (CI翻译)
In the fourth test case, no ducks need to be added. Ja only needs to reorder the piles into increasing order. The minimum number of adjacent swaps is $15$.
在第四个测试用例中,不需要添加鸭子。Ja 只需要将各堆重新排列为递增顺序。相邻交换的最小次数为 $15$。AI (CI翻译)
In the fifth test case, no ducks need to be added. Again, Ja only needs to reorder the piles into increasing order. The minimum number of adjacent swaps is $12$.
在第五个测试用例中,同样不需要添加鸭子。Ja 只需要将各堆重新排列为递增顺序。相邻交换的最小次数为 $12$。AI (CI翻译)
样例
10
3
1 2 2
1 3 5
3
2 2 1
1 2 3
2
5 1
2 4
6
6 5 4 3 2 1
1 2 3 4 5 6
7
4 7 1 6 2 5 3
1 2 3 4 5 6 7
2
2 1
2 3
4
3 2 2 1
1 2 3 4
4
4 3 2 1
1 3 4 5
5
1 5 4 3 2
2 3 4 5 6
5
10 3 8 6 9
3 6 8 9 10
0
2
-1
15
12
0
4
4
3
5
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。