Codeforces 134A-Average Numbers(思维)
2014-09-30 12:57
323 查看
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
output
input
output
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; }
相关文章推荐
- codeforces-660【思维】
- CodeForces 554B Ohana Cleans Up(思维)
- Codeforces 315C Sereja and Contest【思维】
- CodeForces 822C(思维)
- Codeforces-868C - Qualification Rounds - 思维
- Codeforces 828C String Reconstruction【思维+并查集】
- codeforces 777D. Cloud of Hashtags (逆序思维)
- 【CodeForces】554B - Ohana Cleans Up(思维)
- Codeforces 115 B Lawnmower【思维】
- codeforces 552C Vanya and Scales 【思维】
- Codeforces 876E National Property【思维建图+2-sat+输出可行解】好题~
- Codeforces 782C Andryusha and Colored Balloons【思维+Dfs暴力染色】
- Codeforces 525B Pasha and String 【思维】
- Codeforces--525B--Pasha and String(思维)
- Codeforces 330D Biridian Forest【思维+Bfs】
- CodeForces - 785B Anton and Classes (思维题)
- Codeforces 500C:New Year Book Reading(思维+贪心?)
- CodeForces - 729D Sea Battle(思维题)
- CodeForces - 711C Coloring Trees(DP)(思维)
- Codeforces 404C Restore Graph【思维】