题意:
由正整数ai组成的长度为n的序列。假如去掉第j个数后,其余的数满足其中一个数等于其他数的和,求所有符合题意的j。
1 <= n <= 2e5,1 <= ai <= 1e6。
题解:
1.一个数等于其他数的和 等价于 这个序列最大数等于其他数的和。
2.结构体存储序列的数字和每个数字的序号,先将序列从大到小排序,假如去掉的数是第一个数的话,那么剩余序列的最大数就是新序列的第二个数,否则就是第一个数。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<stack>
#define N 200005
#define mod 1000000007
using namespace std ;
struct node
{
int num ;
int id ;
} a[N] ;
bool cmp(node a , node b)
{
return a.num > b.num ;
}
int main()
{
int i , j , cnt = 0 ;
int n , b[N] ;
int max1 ;
long long sum = 0 , temp ;
scanf("%d" , &n) ;
for(i = 1 ; i <= n ; i ++)
{
scanf("%d" , &a[i]) ;
a[i].id = i ;
sum += a[i].num ;
}
sort(a + 1 , a + n + 1 , cmp) ;
for(i = 1 ; i <= n ; i ++)
{
temp = sum ;
temp -= a[i].num ;
if(i == 1)
max1 = a[2].num ;
else
max1 = a[1].num ;
if(max1 * 2 == temp)
b[++cnt] = a[i].id ;
}
printf("%d\n" , cnt) ;
for(i = 1 ; i <= cnt ; i ++)
printf("%d " , b[i]) ;
}