QOJ.ac

QOJ

Time Limit: 2.0 s Memory Limit: 256 MB Total points: 100

#17222. Balancing the Barns

Statistics

Farmer John 有 $N$ ($1\le N\le 5\cdot 10^4$) 个谷仓沿一条道路排列。第 $i$ 个谷仓包含 $a_i$ 捆干草和 $b_i$ 袋饲料 ($0\le a_i,b_i\le 10^9$)。

Bessie 一直在抱怨谷仓之间的不平等。她将农场的“不平衡度”定义为任意谷仓中干草的最大值与任意谷仓中饲料的最小值之差。形式化地,不平衡度为 $\max(a) - \min(b)$。

为了解决 Bessie 的担忧,Farmer John 可以恰好执行 $K$ ($1\le K\le 10^{18}$) 次转移。在每次转移中,他选择一个谷仓 $i$,卖掉其中的一捆干草,并为同一个谷仓买入一袋新的饲料。注意,农场中的数量可以是负数(他不怕负债)。形式化地,你需要进行 $K$ 次操作,每次选择一个下标 $i\in [1,N]$,将 $a_i$ 减 $1$,并将 $b_i$ 加 $1$。

请帮助 Farmer John 确定在执行恰好 $K$ 次转移后,可能达到的最小不平衡度。

输入格式

第一行包含 $T$ ($1 \leq T \leq 10^3$),表示独立测试用例的数量。

每个测试用例的第一行包含 $N$ 和 $K$。

下一行包含 $a_1\dots a_N$。

下一行包含 $b_1\dots b_N$。

所有测试用例的 $N$ 之和不超过 $5 \cdot 10^4$。

输出格式

对于每个测试用例,输出一个整数,表示执行 $K$ 次操作后 $\max(a) - \min(b)$ 的最小值。

样例

输入 1

4
1 10
5
3
2 6
100 96
0 4
3 3
1 1 2
0 0 1
3 3
1 2 2
0 1 1

输出 1

-18
90
0
0

说明 1

在第一个测试用例中,Farmer John 可以将 $10$ 捆干草从谷仓 $1$ 转移为饲料。这使得 $a = [-5]$ 且 $b = [13]$。不平衡度为 $\max(a) - \min(b) = -5 - 13 = -18$。

在第二个测试用例中,Farmer John 可以从谷仓 $1$ 转移 $5$ 捆干草,从谷仓 $2$ 转移 $1$ 捆干草。这使得 $a = [95, 95]$ 且 $b = [5, 5]$。不平衡度为 $95 - 5 = 90$。这是 Farmer John 能达到的最小不平衡度。

子任务

  • 输入 2-4:$K\le 500$,所有测试用例的 $N$ 之和 $\le 500$
  • 输入 5-8:所有测试用例的 $N$ 之和 $\le 500$
  • 输入 9-13:无额外限制。

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.