CF2238A.Another Puzzle from Papyrus
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are filled with determination.
— Undertale
Papyrus came up with another puzzle for Frisk to solve. Papyrus brought two arrays a and b of length n and allowed the following two operations to be performed:
- choose any index i (1≤i≤n) and change ai to ai−1. The execution time of such an operation is 1 second.
- reorder all elements of array a in any way. The execution time of such an operation is c seconds.
You need to convert array a into array b.
Frisk wants to solve the puzzle as soon as possible. Help Frisk determine the minimum time needed to solve the puzzle. If there is no solution, output −1.
你充满了决心。
——《Undertale》
帕派瑞斯(Papyrus)又为弗里斯克(Frisk)设计了一个谜题。帕派瑞斯带来了两个长度为 n 的数组 a 和 b,并允许执行以下两种操作:
- 选择任意下标 i(1≤i≤n),将 ai 修改为 ai−1。该操作耗时 1 秒。
- 以任意方式重排数组 a 的所有元素。该操作耗时 c 秒。
你需要将数组 a 转换为数组 b。
弗里斯克希望尽快解出该谜题。请帮助弗里斯克确定解出谜题所需的最短时间。若无解,请输出 −1。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤500). The description of the test cases follows.
The first line of each test case contains two integers n and c (1≤n,c≤100) — the length of arrays a and b and the cost of the second operation.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤100) — the elements of the first array.
The third line of each test case contains n integers b1,b2,…,bn (1≤bi≤100) — the elements of the second array.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤500)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 c(1≤n,c≤100)—— 分别表示数组 a 和 b 的长度,以及第二种操作的代价。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤100)—— 表示第一个数组的元素。
每个测试用例的第三行包含 n 个整数 b1,b2,…,bn(1≤bi≤100)—— 表示第二个数组的元素。
输出格式
For each test case, output a single integer representing the minimum number of seconds required to solve the puzzle, or −1 if it is impossible to solve the puzzle.
对于每个测试用例,输出一个整数,表示解决该谜题所需的最少秒数;如果无法解决该谜题,则输出 −1。
输入输出样例
输入#1
6 3 5 5 2 3 2 3 4 3 3 1 2 3 4 5 6 4 4 4 5 2 3 3 5 1 2 6 4 2 4 5 3 6 8 5 8 3 1 2 5 5 11 5 8 11 14 17 16 12 10 10 6 3 5 20 14 20 12 18 17
输出#1
6 -1 3 8 -1 12
说明/提示
In the first test case, it is impossible to transform a into b using only subtraction because a2<b2. Let's rearrange the elements of array a as follows: [5,2,3]⇒[2,3,5]. Now it is enough to subtract one from a3, and we get that array a becomes equal to array b in 5+1=6 seconds.
In the second test case, all elements of a are less than all elements of b, which means a cannot be transformed into b.
In the third test case, you can choose not to rearrange the elements and get an answer of 3. If you rearrange them at least once, the answer will be at least 4, so the optimal answer is 3 seconds.
In the sixth test case, the array can be rearranged as follows: [14,20,20]. It can be seen that the cost will then be 5+(14−12)+(20−18)+(20−17)=12.
在第一个测试用例中,仅使用减法操作无法将 a 变换为 b,因为 a2<b2。让我们将数组 a 的元素重新排列如下:[5,2,3]⇒[2,3,5]。此时只需对 a3 减去 1,即可使数组 a 变为与数组 b 相等,总耗时为 5+1=6 秒。
在第二个测试用例中,a 的所有元素均小于 b 的所有元素,这意味着 a 无法被变换为 b。
在第三个测试用例中,你可以选择不重排元素,从而得到答案 3。若至少重排一次,答案至少为 4,因此最优答案为 3 秒。
在第六个测试用例中,数组可重排为:[14,20,20]。此时总代价为 5+(14−12)+(20−18)+(20−17)=12。
输入解题思路,AI测评打分。不知道怎么写?