码迷,mamicode.com
首页 > 其他好文 > 详细

[模拟]Decrease (Judge ver.)

时间:2018-07-08 22:08:26      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:scan   HERE   lld   determine   namespace   integer   mat   ret   最大的   

题目描述

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\n",ans);
    return 0;
}

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

[模拟]Decrease (Judge ver.)

标签:scan   HERE   lld   determine   namespace   integer   mat   ret   最大的   

原文地址:https://www.cnblogs.com/lllxq/p/9281278.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!