CodeForces1077C Good Array

CodeForces1077C Good Array

Description

Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array a=[1,3,3,7] is good because there is the element a4=7 which equals to the sum 1+3+3.You are given an array a consisting of n integers. Your task is to print all indices j of this array such that after removing the j-th element from the array it will be good (let's call such indices nice).

For example, if a=[8,3,5,2], the nice indices are 1 and 4:

  • if you remove a1, the array will look like [3,5,2] and it is good;
  • if you remove a4, the array will look like [8,3,5] and it is good.

You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.

Input

The first line of the input contains one integer n ( 2≤n≤2⋅105 ) — the number of elements in the array a.

The second line of the input contains n integers a1,a2,…,an (1≤ai≤106) — elements of the array a.

Output

In the first line print one integer k — the number of indices j of the array a such that after removing the j-th element from the array it will be good (i.e. print the number of the nice indices).

In the second line print k distinct integers j1,j2,…,jk in any order — nice indices of the array a.

If there are no such indices in the array a, just print 0 in the first line and leave the second line empty or do not print it at all.

Examples

​ Input

5
2 5 1 2 2

​ Output

3
4 1 5

​ Input

4
8 3 5 2

​ Output

2
1 4 

​ Input

5
2 1 2 4 3

​ Output

0

Note

In the first example you can remove any element with the value 2 so the array will look like [5,1,2,2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5=1+2+2).

In the second example you can remove 8 so the array will look like [3,5,2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5=3+2). You can also remove 2 so the array will look like [8,3,5]. The sum of this array is 16 and there is an element equals to the sum of remaining elements (8=3+5).

In the third example you cannot make the given array good by removing exactly one element.

题解

题意

问,对于数组内的每个数,是否存在去掉其他一个数,使得剩余的值的和为该值

思路

对于每个值,首先维护出,除了该值的和,然后对于差值进行二分查找。 lower_bound找到的是首个大于等于的值,要判断是否相等,FST failed了……

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

const int MAXN = 1e6+10;
vector<int> v1[MAXN];

int a[MAXN];
int cm[MAXN];
ll s[MAXN];
ll ans = 0;
set<int> s1;
set<int>::iterator iter;

void init(){
	memset(a,0,sizeof(a));
	memset(cm,0,sizeof(cm));
	memset(s,0,sizeof(s));
	for(int i=0;i<MAXN;i++){
		v1[i].clear();
	}
	s1.clear();
	ans = 0;
}

int main(){
	int N;
	while(~scanf("%d",&N)){
		init();
		for(int i=1;i<=N;i++){
			scanf("%d",&a[i]);
			cm[i] = a[i];
			ans += a[i];
			v1[a[i]].push_back(i);
		}
		for(int i=1;i<=N;i++){
			s[i] = ans- a[i];
		}
		sort(cm+1,cm+N+1);
		for(int i=1;i<=N;i++){
			ll cha = s[i] - a[i];
			int pos = lower_bound(cm+1,cm+1+N,cha) - cm;
            for(int j=0;j<v1[cm[pos]].size()&&(v1[cm[pos]].size()>1||cm[pos]!=a[i])&&cm[pos]==cha;j++){
				s1.insert(v1[cm[pos]][j]);
			}
		}
		ll flag = s1.size();
		if(flag==0){
			printf("0
");
		}else{
			printf("%lld
",flag);
			for(iter=s1.begin();iter!=s1.end();iter++){
				printf("%d ",*iter);
			}
			printf("
");
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/caomingpei/p/9974627.html