Educational Codeforces Round 98 (Rated for Div. 2)------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.

题意

从n堆玩具中选出一堆,然后将这一堆分给其他n - 1堆,问为了使其他n- 1堆玩具数目相等,还需添加多少个玩具

题解

两个条件, 每堆玩具数目k
1、sum % (n - 1) == 0, 可向上取整, k1
2、如果选的堆不是最大的堆,那么k2必须等于max
所以,综上,k = max(k1, k2)
注意:会爆int,用longlong

AC代码

#include<stdio.h>
#include<algorithm>
using namespace std;
#define ll long long
int main()
{
    int t; scanf("%d", &t);
    while(t--){
        int n; scanf("%d", &n);
        ll mx = -1;
        ll sum = 0;
        for(int i = 0; i <n; i++){
            ll x; scanf("%lld", &x);
            sum += x;
            mx = max(mx, x);
        }
        ll p;
        if(sum % (n - 1) == 0)
            p = sum / (n  - 1);
        else
            p = (sum / (n - 1)) + 1;
        ll res = max(p, mx) * (n - 1) - sum;
        printf("%lld\n", res);
    }
    return 0;
}
posted @ 2020-11-21 09:30  AC_沫离  阅读(48)  评论(0)    收藏  举报