【模拟】Decrease (Judge ver.)

编程入门 行业动态 更新时间:2024-10-22 13:33:58

【模拟】<a href=https://www.elefans.com/category/jswz/34/1739387.html style=Decrease (Judge ver.)"/>

【模拟】Decrease (Judge ver.)

【模拟】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;
}

更多推荐

【模拟】Decrease (Judge ver.)

本文发布于:2024-03-23 14:42:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1739385.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Decrease   Judge   ver

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!