You are given a sequence of positive integers a1, a2, ..., an. Find all such indices i, that the i-th element equals the arithmetic mean of all other elements (that is all elements except for this one).
The first line contains the integer n (2 ≤ n ≤ 2·105). The second line contains elements of the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000). All the elements are positive integers.
Print on the first line the number of the sought indices. Print on the second line the sought indices in the increasing order. All indices are integers from 1 to n.
If the sought elements do not exist, then the first output line should contain number 0. In this case you may either not print the second line or print an empty line.
5 1 2 3 4 5
1 3
4 50 50 50 50
4 1 2 3 4
题意:给出n个数,对于第i个数,看a[i]是否等于其他数的算数平均数 n<2*10^5 一开始觉得很难写,因为要暴力的话,最开始想到的是O(n*n)的算法,外循环枚举a[i],内循环求平均数 ,但后来一想求平均数很简单,只需要让 这些数的总和 (sum-a[i])/(n-1) 即可 有一个坑点是平均数不一定为整数,但数组中的每个元素都一定是整数。
<pre name="code" class="html">#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <string> #include <vector> using namespace std; #define LL long long int a[200050],ans[200050]; int main() { int n; while(scanf("%d",&n)!=EOF) { int sum=0; for(int i=1;i<=n;i++) { scanf("%d",a+i); sum+=a[i]; } int cnt=0; for(int i=1;i<=n;i++) { int t=sum-a[i]; if(t%(n-1)==0) if(a[i]==t/(n-1)) ans[cnt++]=i; } printf("%d\n",cnt); if(cnt) { for(int i=0;i<cnt;i++) if(i!=cnt-1) printf("%d ",ans[i]); else printf("%d\n",ans[i]); } } return 0; }

此博客介绍了一个算法问题,即在给定的一系列正整数中,找到那些数值等于其他所有数值算术平均数的索引。通过提供输入样例和详细解释,展示了如何解决这一挑战。
1318

被折叠的 条评论
为什么被折叠?



