Description
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.
Sample Input
2 2 1
1
3 1 4 1
3
题目大意:给出一个序列,进行操作,每次选取两个数字,一个+1一个-1,问最后最多能有多少个数字是相等的。比如样例2可以变成2 2 2,所以输出3.
这是一道思维题,然后像我这种脑残就死活写不出来,想了好久才明白一个事情。
其实一个数字+1,一个数字-1,最后保证整个序列的和是一直不变的嘛,所以你需要判断是就是把这些数字加起来得到一个sum后,对n进行取余,如果余数位0,就说明大家可以分得刚刚好,最大值就是n,输出。如果不为0,很简单啊,我现在想懂了很简单,就是选出一个数字,你把sum全部加到这个数字上去好了,然后其他数字全部变为0,牺牲了这个数字,得到的最大值就是n-1.所以只有两种答案。
所以代码其实真的超级短的说:
#include<cstdio>
using namespace std;
int main()
{
long long i,n,t;
long long sum;
scanf("%I64d",&n);
sum = 0;
for(i = 0; i < n; i++)
{
scanf("%I64d", &t);
sum += 10000 + t;
}
if(sum % n == 0)
printf("%d\n",n);
else
printf("%d\n", n - 1);
return 0;
}