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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值