QOJ.ac

QOJ

时间限制: 2.0 s 内存限制: 512 MB 总分: 100 可 Hack ✓

#16854. 两个数组

统计

令数组 $d$ 中的最大值表示为 $\max(d)$,最小值表示为 $\min(d)$。

给定两个长度为 $n$ 的数组 $a$ 和 $b$。在一次操作中,你可以选择一个索引 $1 \le i \le n$,并同时将元素 $a_i$ 和 $b_i$ 增加 $1$:即 $a_i = a_i + 1$,$b_i = b_i + 1$。需要通过这些操作使得以下两个条件同时满足:

  • $\max(a) - \min(a) \le x$,
  • $\max(b) - \min(b) \le y$。

求达到同时满足上述条件所需的最小操作次数,如果无法做到,则输出 $-1$。

输入格式

每个测试点包含多个测试用例。第一行包含一个整数 $t$ — 测试用例的数量 ($1 \le t \le 10^5$)。接下来是每个测试用例的描述。

每个测试用例的第一行包含三个整数:$n, x, y$ ($1 \le n \le 10^5$, $0 \le x, y \le 10^9$)。

每个测试用例的第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$ — 数组 $a$ 的元素 ($-10^9 \le a_i \le 10^9$)。

每个测试用例的第三行包含 $n$ 个整数 $b_1, b_2, \dots, b_n$ — 数组 $b$ 的元素 ($-10^9 \le b_i \le 10^9$)。

保证所有测试用例中 $n$ 的总和不超过 $10^5$。

输出格式

对于每个测试用例,输出一个整数 — 满足两个条件所需的最小可能操作次数。如果无法同时满足两个条件,则输出 $-1$。

样例

输入 1

5
4 2 3
-1 -2 -3 -4
-1 -2 -3 -4
3 3 2
1 6 4
1 4 1
4 0 3
0 2 1 2
0 2 3 3
5 2 1
-1 0 1 2 3
2 2 2 2 2
3 66 77
235 -111 9
100 -200 -100

输出 1

1
3
3
-1
440

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.