【模拟】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
给你一个数列,找到这个数列的最大值,如果最大值大于n,那么最大值减n,其余各项加一,最后使所有项小于n,求最小操作次数
我们可以对模拟操作进行优化一次循环即可把最大值其变成小于n(后边的操作有可能再次将他变成大于等于n)
注意:操作次数的变量用long long
错了5次~~
代码
#include <bits/stdc++.h>
using namespace std;typedef long long ll;ll a[100];int main() {
int n;cin >> n;ll cnt = 0;for(int i = 1; i <= n; i++) {
cin >> a[i];}while(1) {
ll tmax = a[1], id = 1;for(int i = 2; i <= n; i++) {
if(a[i] > tmax) {
tmax = a[i];id = i;}//找最大值}if(tmax <= n - 1) {
break;//结束循环}for(int i = 1; i <= n; i++) {
if(i == id) a[i] = tmax % n;//对模拟的优化else a[i] += tmax / n;}cnt += (tmax / n);//操作的次数}cout<<cnt;return 0;
}