Educational Codeforces Round 98 (Rated for Div. 2) B. Toy Blocks 贪心

You are asked to watch your nephew who likes to play with toy blocks in a strange way.

He has n boxes and the i-th box has ai blocks. His game consists of two steps:

he chooses an arbitrary box i;
he tries to move all blocks from the i-th box to other boxes.
If he can make the same number of blocks in each of n−1 other boxes then he will be happy, otherwise, will be sad. Note that your nephew can only move the blocks from the chosen box to the other boxes; he cannot move blocks from the other boxes.
You don’t want to make your nephew sad, so you decided to put several extra blocks into some boxes in such a way that no matter which box i he chooses he won’t be sad. What is the minimum number of extra blocks you need to put?

Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.

The first line of each test case contains the integer n (2≤n≤105) — the number of boxes.

The second line of each test case contains n integers a1,a2,…,an (0≤ai≤109) — the number of blocks in each box.

It’s guaranteed that the sum of n over test cases doesn’t exceed 105.

Output
For each test case, print a single integer — the minimum number of blocks you need to put. It can be proved that the answer always exists, i. e. the number of blocks is finite.

Example
inputCopy
3
3
3 2 2
4
2 2 3 2
3
0 3 0
outputCopy
1
0
3
Note
In the first test case, you can, for example, put one extra block into the first box and make a=[4,2,2]. If your nephew chooses the box with 4 blocks, then we will move two blocks to the second box and two blocks to the third box. If he chooses the box with 2 blocks then he will move these two blocks to the other box with 2 blocks.

In the second test case, you don’t need to put any extra blocks, since no matter which box your nephew chooses, he can always make other boxes equal.

In the third test case, you should put 3 extra blocks. For example, you can put 2 blocks in the first box and 1 block in the third box. You’ll get array a=[2,3,1].
排序后从最小的开始分配,如果最小项小于最大项,则将所有项都可以填充最大项,如果最小项与最大项相等就把第一个平均分配给其他项,使得其他项个数都相同

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <functional>
#include <vector>
#include <stack>
#include <set>
#define int long long
using namespace std;
typedef long long ll;
const int maxn=2e5+50;
const int inf=0x3f3f3f3f3f3f3f3f;
const int MOD=1e9+7;
const int HASH=131;

int a[maxn];

signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        sort(a+1,a+n+1);
        int maxx=a[n];
        int need=0;
        for(int i=1;i<=n;i++)
        {
            need+=(maxx-a[i]);
        }
        int ans=0;
        for(int i=1;i<n;i++)
        {
            need-=(maxx-a[i]);
            if(a[i]>need)
            {
                ans=max(ans,(n-1-(a[i]-need)%(n-1))%(n-1));
            }
            else
            {
                ans=max(ans,need-a[i]);
            }
            need+=(maxx-a[i]);
        }
        need=0;
        for(int i=1;i<n;i++)
        {
            need+=(a[n-1]-a[i]);
        }
        if(a[n]>need)
        {
            ans=max(ans,(n-1-(a[n]-need)%(n-1))%(n-1));
        }
        else
        {
            ans=max(ans,need-a[n]);
        }
        cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值