Codeforces 246B Increase and Decrease 【数学】

本文介绍Codeforces246B题目的解题思路与实现方法,通过操作使数组中元素尽可能相等,讨论了操作次数不限时,如何最大化相同元素的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:Codeforces 246B Increase and Decrease

B. Increase and Decrease
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarpus has an array, consisting of n integers a1, a2, …, an. Polycarpus likes it when numbers in an array match. That’s why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times:

he chooses two elements of the array ai, aj (i ≠ j);
he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai = ai + 1 and aj = aj - 1.
The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times.

Now he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus.

Input
The first line contains integer n (1 ≤ n ≤ 105) — the array size. The second line contains space-separated integers a1, a2, …, an (|ai| ≤ 104) — the original array.

Output
Print a single integer — the maximum number of equal array elements he can get if he performs an arbitrary number of the given operation.

Examples
input
2
2 1
output
1
input
3
1 4 1
output
3

题意:给定n个数,每次可以将其中不相同的两个数一个加一,一个减一。问你最多可以得到多少个相同的数。

思路:整除时显然可以得到n个,而不能整除时可以得到n-1个。

AC代码:

#include <iostream>
using namespace std;
int main()
{
    int n; cin >> n;
    int sum = 0;
    for(int i = 1; i <= n; i++) {
        int a; cin >> a;
        sum += a;
    }
    if(sum % n == 0) {
        cout << n << endl;
    }
    else {
        cout << n - 1 << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值