链接:https://codeforces.com/contest/1452/problem/B
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
input
3
3
3 2 2
4
2 2 3 2
3
0 3 0
output
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].
题意
给你一个序列,你可以在某些元素加点值,可以使每个元素都可以分给其他元素,使剩下的都一样,问最少要加的值是多少。
思路
t = max (Max,⌈sum/(n-1)⌉)
求平均数,得向上取整,平均数是⌈sum/(n-1)⌉,然后和序列中最大值作比较,这就是你最后分配完每个数的值,这种情况下肯定最小,然后求差值,就是答案。
简单思维题,还是得拼手速。
代码
#include <bits/stdc++.h>
typedef long long ll;
const ll mod =1e9 + 7;
using namespace std;
namespace fastIO {
inline void input(ll& res) {
char c = getchar();res = 0;int f = 1;
while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
res = f ? res : -res;
}
inline ll qpow(ll a, ll b) {
ll ans = 1, base = a;
while (b) {
if (b & 1) ans = (ans * base % mod +mod )%mod;
base = (base * base % mod + mod)%mod;
b >>= 1;
}
return ans;
}
}
using namespace fastIO;
const int N = 1e5+5;
ll Case,n;
ll a[N];
ll sum,maxx;
void solve(){
input(n);
ll ss = 0;
sum = 0;
maxx = 0;
for(int i=1;i<=n;i++){
input(a[i]);
ss += a[i];
sum += a[i];
maxx = max(a[i],maxx);
}
if(ss%(n-1)){
ss/=(n-1);
ss++;
}
else{
ss/=(n-1);
}
ss=max(ss,maxx);
sum=ss*(n-1)-sum;
printf("%lld\n",sum);
}
int main(){
Case=1;
input(Case);
while(Case--){
solve();
}
return 0;
}
/*
3
3
3 2 2
4
2 2 3 2
3
0 3 0
*/