题目描述
We have a sequence of length N consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes N−1 or smaller. (The operation is the same as the one in Problem D.)
Determine the largest element in the sequence (if there is more than one, choose one). Decrease the value of this element by N, and increase each of the other elements by 1.
It can be proved that the largest element in the sequence becomes N−1 or smaller after a finite number of operations.
You are given the sequence ai. Find the number of times we will perform the above operation.
Constraints
2≤N≤50
0≤ai≤1016+1000
输入
Input is given from Standard Input in the following format:
N
a1 a2 ... aN
输出
Print the number of times the operation will be performed.
样例输入
4
3 3 3 3
样例输出
0
首先做这个题目不要被他的描述给绕进去了,题目虽然说的是每次找最大的数-N然后其他的数+1,但是这样在写程序的时候会发现很复杂,程序做了很多重复性的工作,通过认真分析,完全可以把最大的数一次性的减到N以下,然后计算减的次数,然后其他的数就加上减的次数,会发现这两个操作其实是等价的,显然后者会让程序算法优化很多,下面是程序代码
#include<iostream>
#include<vector>
using namespace std;
typedef long long int ll;
int main(){
bool flag=true;//下方程序循环判断的标志,如果最大的值小于n的话,那么就结束循环
ll ans=0;
vector<ll>ve;
int n;
cin>>n;
ll temporary;
int position;
ll x;
for(int i=0;i<n;i++){
cin>>x;
ve.push_back(x);//把数据读入到一个不定长数组里
}
ll max;
while(flag){
max=ve[0];//首先假定第一个元素为最大值
position=0;//同时也假定最大元素的坐标是0;
for(int i=1;i<n;i++){
if(ve[i]>max){
max=ve[i];//开始寻找数组中的最大值,并记录它的位置
position=i;
}
}
if(max>=n){
temporary=max/n;//计算减到n以下需要的次数
ve[position]%=n;
ans+=temporary;
for(int i=0;i<n;i++){
if(position==i)
continue;
ve[i]+=temporary;//把其他的元素加上上边减到n一下需要的次数
}
}
else{
cout<<ans<<endl;
flag=false;
}
}
return 0;
}