Codeforces Round #521 (Div. 3) C. Good Array

C. Good Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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]a=[1,3,3,7] is good because there is the element a4=7a4=7 which equals to the sum 1+3+31+3+3 .

You are given an array aa consisting of nn integers. Your task is to print all indices jj of this array such that after removing the jj -th element from the array it will be good (let's call such indices nice).

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

  • if you remove a1a1 , the array will look like [3,5,2][3,5,2] and it is good;
  • if you remove a4a4 , the array will look like [8,3,5][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 nn (2n21052≤n≤2⋅105 ) — the number of elements in the array aa .

The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (1ai1061≤ai≤106 ) — elements of the array aa .

Output

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

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

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

Examples
Input
Copy
5
2 5 1 2 2
Output
Copy
3
4 1 5
Input
Copy
4
8 3 5 2
Output
Copy
2
1 4 
Input
Copy
5
2 1 2 4 3
Output
Copy
0

Note

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

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

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

首先思路是预处理一个数组maxx,维护除了i位置a[i]以外的最大值。然后求sum=sigma(a[i]),遍历一遍,if(maxx[i]==(sum-a[i]-maxx[i]))

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn=200010;
 5 
 6 
 7 int a[maxn],n,maxl[maxn],maxr[maxn],maxx[maxn],ans[maxn];
 8 ll sum;
 9 int main() {
10     scanf("%d",&n);
11     for(int i=1;i<=n;i++) {
12         scanf("%d",&a[i]);
13         sum+=a[i];
14     }
15     for(int i=2;i<=n;i++) {
16         maxl[i]=max(a[i-1],maxl[i-1]);
17     }
18     for(int i=n-1;i>=1;i--) {
19         maxr[i]=max(a[i+1],maxr[i+1]);
20     }
21     for(int i=1;i<=n;i++) maxx[i]=max(maxl[i],maxr[i]);
22     int cnt=0;
23     for(int i=1;i<=n;i++) {
24         if(maxx[i]==sum-a[i]-maxx[i]) {
25             ans[cnt++]=i;
26         }
27     }
28     if(!cnt) printf("0
");
29     else {
30         printf("%d
",cnt);
31         for(int i=0;i<cnt;i++) printf("%d ",ans[i]);
32     }
33 }

本以为这样挺好的,后来发现可以更简单,看了题解,若有所思。 因为除去a[i]以后,如果存在这样的好位置,那么剩余的数之和一定是偶数,(因为就是2倍的最大值)。然后判断最大值存不存在即可。

代码为正解:


 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 const int MAX = 1e6;
 6 
 7 int main() {
 8 #ifdef _DEBUG
 9     freopen("input.txt", "r", stdin);
10 //    freopen("output.txt", "w", stdout);
11 #endif
12     
13     int n;
14     cin >> n;
15     vector<int> a(n);
16     vector<int> cnt(MAX + 1);
17     for (int i = 0; i < n; ++i) {
18         cin >> a[i];
19         ++cnt[a[i]];
20     }
21     long long sum = accumulate(a.begin(), a.end(), 0ll);
22     
23     vector<int> ans;
24     for (int i = 0; i < n; ++i) {
25         sum -= a[i];
26         --cnt[a[i]];
27         if (sum % 2 == 0 && sum / 2 <= MAX && cnt[sum / 2] > 0) {
28             ans.push_back(i);
29         }
30         sum += a[i];
31         ++cnt[a[i]];
32     }
33     
34     cout << ans.size() << endl;
35     for (auto it : ans) cout << it + 1 << " ";
36     cout << endl;
37     
38     return 0;
39 }



原文地址:https://www.cnblogs.com/ACMerszl/p/9991053.html