QOJ.ac

QOJ

実行時間制限: 1 s メモリ制限: 256 MB 満点: 100 ハック可能 ✓

#6702. 桶中的石头

統計

地面上有 $n$ 个桶,其中第 $i$ 个桶里有 $a_i$ 颗石头。每次操作可以执行以下两种操作之一:

  • 从一个非空桶中移走一颗石头。
  • 将一颗石头从一个桶(必须非空)移动到另一个桶(可以是空桶)。

请问最少需要进行多少次操作,才能使所有桶中的石头数量相等?

输入格式

输入包含多组测试数据。第一行包含一个整数 $T$,表示测试数据的组数。对于每组测试数据:

第一行包含一个整数 $n$ ($1 \le n \le 10^5$),表示桶的数量。 第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$),表示每个桶中的石头数量。

保证所有测试数据的 $n$ 之和不超过 $10^6$。

输出格式

对于每组测试数据,输出一行,包含一个整数,表示使所有桶中石头数量相等所需的最少操作次数。

样例

样例输入 1

4
3
1 1 0
4
2 2 2 2
3
0 1 4
1
1000000000

样例输出 1

2
0
3
0

说明

对于第一个样例,可以将前两个桶中的石头全部移走。 对于第二个样例,由于所有桶中已经包含相同数量的石头,因此不需要任何操作。 对于第三个样例,可以将第 3 个桶中的 1 颗石头移动到第 1 个桶,然后从第 3 个桶中移走 2 颗石头。

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.