[模拟]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,其余加1,直到所有数都小于n,其实这个操作等价于每次取最大的直接减到n以下,其余加上1*减n的次数;
AC代码:
#include <iostream>
#include<cstdio>
#include<algorithm>
typedef long long ll;
using namespace std;

ll a[55];

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    ll ans=0;
    for(;;){
        ll maxn=a[1];int ind=1;
        for(int i=2;i<=n;i++) if(a[i]>maxn) maxn=a[i],ind=i;
        if(maxn<=n-1) break;
        for(int i=1;i<=n;i++){
            if(i==ind) a[i]=maxn%n;
            else a[i]+=maxn/n;
        }
        ans+=(maxn/n);
    }
    printf("%lld
",ans);
    return 0;
}

直接模拟不可行时,可尝试一种等价的方式模拟

转载请注明出处:https://www.cnblogs.com/lllxq/
原文地址:https://www.cnblogs.com/lllxq/p/9281278.html