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以下,然后计算减的次数,然后其他的数就加上减的次数,会发现这两个操作其实是等价的,显然后者会让程序算法优化很多,下面是程序代码

#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;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值