Decrease (Judge ver.)

题目描述

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以下,然后其余元素加上。

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define range(i,a,b) for(int i=a;i<=b;++i)
#define LL long long
#define rerange(i,a,b) for(int i=a;i>=b;--i)
#define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
using namespace std;
int n;
LL num[55],ans;
void init(){
    cin>>n;
    range(i,1,n)cin>>num[i];
}
void solve(){
    while(true){
        LL MAX=-2398572,pos;
        range(i,1,n)if(MAX<num[i]){
            MAX=num[i];
            pos=i;
        }
        if(MAX<n)break;
        range(i,1,n){
            if(i!=pos)num[i]+=MAX/n;
            else num[i]%=n;
        }
        ans+=MAX/n;
    }
    cout<<ans<<endl;
}
int main() {
    init();
    solve();
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/Rhythm-/p/9318764.html