ZOJ - 4118:Stones in the Bucket

There are  buckets on the ground, where the -th bucket contains  stones. Each time one can perform one of the following two operations:

  • Remove a stone from one of the non-empty buckets.
  • Move a stone from one of the buckets (must be non-empty) to any other bucket (can be empty).

 

What's the minimum number of times one needs to perform the operations to make all the buckets contain the same number of stones?

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains an integer  (), indicating the number of buckets.

The second line contains  integers  (), indicating the number of stones in the buckets.

It's guaranteed that the sum of  of all test cases will not exceed .

Output

For each test case output one line containing one integer, indicating the minimum number of times needed to make all the buckets contain the same number of stones.

Sample Input

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

Sample Output

2
0
3
0

Hint

For the first sample test case, one can remove all the stones in the first two buckets.

For the second sample test case, as all the buckets have already contained the same number of stones, no operation is needed.

For the third sample test case, one can move 1 stone from the 3rd bucket to the 1st bucket and then remove 2 stones from the 3rd bucket.

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
long long sum,mid,num;
long long a[100010];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        memset(a,0,sizeof(a));
        sum = 0;
        num = 0;
        scanf("%d",&n);
        for(int i = 0;i<n;++i)
        {
            scanf("%lld",&a[i]);
            sum += a[i];
        }
        mid = sum/n;
        for(int i = 0;i<n;++i)
        {
            if(a[i]<=mid)
            {
                continue;
            }
            num += a[i]-mid;
        }
        printf("%lld\n",num);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朱事顺利、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值