Educational Codeforces Round 98 B. Toy Blocks
阿新 • • 發佈:2020-12-04
原題連結:https://codeforces.com/contest/1452/problem/B
題意:有n個盒子,每個盒子裡面有a[i]個球,對於任意一個盒子,可以將裡面的球全部發出來,分配給其餘 n-1 個盒子使得 其餘 n-1 個盒子球的數量要相等。現在求:向n個盒子中最少新增幾個球才能實現上述操作
思路:
由於要每個數都要滿足。
設原總數為sum,加x個後滿足要求
那麼有:
(sum+x)%(n-1)==0
(sum+x)/(n-1)==avg
設ai被分掉,在分掉之前,剩下的n-1個的最大值為maxx,那麼在分ai的時候,這個maxx可能得到也可能得不到
那麼有avg>=maxx
這個maxx,至少是原數列的最大值,這樣才能保證每個數字都可以滿足條件。
則x>=maxx*(n-1)-sum
x可能會<0
那麼x自加n-1,直到>=0即可。但是while會超時,所以套這個式子把它變成正數:
md=(md%(n-1)+n-1)%(n-1);
程式碼:
#include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<stack> using namespace std; typedef long long ll; const int maxn=2e5+10; int main() {int t; cin>>t; while(t--) { ll n; cin>>n; ll x; ll sum=0; ll maxx=0; for(int i=1;i<=n;i++) { cin>>x; sum+=x; maxx=max(maxx,x); } ll md=maxx*(n-1)-sum; if(md<0) { md=(md%(n-1)+n-1)%(n-1); } cout<<md<<endl; } }