Description:
题目大意:有 n 个盒子,第 i 个盒子有 ai个木块,有两步操作:
- 选择一个盒子
- 将选中的第 i 个盒子中的所有木块放多其他所有盒子中
希望将 n-1 个盒子中的木块数量都相等,不能从其他盒子拿出木块,要求放入最小的额外木块,使得无论选择哪一个盒子都能使 n-1 个盒子的木块数量相同。
解题思路:
算法标签:贪心
首先,如果 n-1 个盒子木块数相同,那么至少能被总共木块数 sum 整除。所以其他盒子中的木块数至少为 [
s
u
m
n
−
1
\frac{sum}{n-1}
n−1sum] 。因为不能移除木块,所以最大木块数不会改变,每个其他盒子木块数至少等于最大木块数 maxx。
当 sum
≤
\leq
≤ (n-1)
×
\times
× maxx,添加的额外木块数即为:(n-1)
×
\times
× maxx - sum
当 sum
>
>
> (n-1)
×
\times
× maxx,说明多出来了,只需在剩下的(n-1-sum%(n-1))个盒子各添加一个木块即可。
代码:
// TSWorld
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
using namespace std;
#define LL long long
const int N = 200005;
int main()
{
int T = 0;
int n = 0;
int number = 0;
int maxx = 0;
LL sum = 0;
cin>>T;
while(T--) {
maxx = 0,sum = 0;
scanf("%d",&n);
for(int i = 1;i <= n;i++) {
scanf("%d",&number);
sum += number;
maxx = max(maxx,number);
}
if(sum <= 1LL*(n-1)*maxx) {
cout<<1LL*(n-1)*maxx - sum<<endl;
}
else {
LL add = sum%(n-1);
if(add != 0)
cout<<1LL*(n-1)-add<<endl;
else
cout<<0<<endl;
}
}
return 0;
}