Codeforces 134A-Average Numbers(思维)

A. Average Numbers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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).

Input

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.

Output

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.

Sample test(s)
input
5
1 2 3 4 5
output
1
3 
input
4
50 50 50 50
output
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;
}
 
  
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值