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 (2≤n≤2⋅1052≤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 (1≤ai≤1061≤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]))

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=; int a[maxn],n,maxl[maxn],maxr[maxn],maxx[maxn],ans[maxn];
ll sum;
int main() {
scanf("%d",&n);
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
sum+=a[i];
}
for(int i=;i<=n;i++) {
maxl[i]=max(a[i-],maxl[i-]);
}
for(int i=n-;i>=;i--) {
maxr[i]=max(a[i+],maxr[i+]);
}
for(int i=;i<=n;i++) maxx[i]=max(maxl[i],maxr[i]);
int cnt=;
for(int i=;i<=n;i++) {
if(maxx[i]==sum-a[i]-maxx[i]) {
ans[cnt++]=i;
}
}
if(!cnt) printf("0\n");
else {
printf("%d\n",cnt);
for(int i=;i<cnt;i++) printf("%d ",ans[i]);
}
}

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

代码为正解:


 #include <bits/stdc++.h>

 using namespace std;

 const int MAX = 1e6;

 int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif int n;
cin >> n;
vector<int> a(n);
vector<int> cnt(MAX + );
for (int i = ; i < n; ++i) {
cin >> a[i];
++cnt[a[i]];
}
long long sum = accumulate(a.begin(), a.end(), 0ll); vector<int> ans;
for (int i = ; i < n; ++i) {
sum -= a[i];
--cnt[a[i]];
if (sum % == && sum / <= MAX && cnt[sum / ] > ) {
ans.push_back(i);
}
sum += a[i];
++cnt[a[i]];
} cout << ans.size() << endl;
for (auto it : ans) cout << it + << " ";
cout << endl; return ;
}


Codeforces Round #521 (Div. 3) C. Good Array的更多相关文章

  1. Codeforces Round #521 (Div. 3) E. Thematic Contests(思维)

    Codeforces Round #521 (Div. 3)  E. Thematic Contests 题目传送门 题意: 现在有n个题目,每种题目有自己的类型要举办一次考试,考试的原则是每天只有一 ...

  2. Codeforces Round #258 (Div. 2) . Sort the Array 贪心

    B. Sort the Array 题目连接: http://codeforces.com/contest/451/problem/B Description Being a programmer, ...

  3. Codeforces Round #521 (Div. 3) D. Cutting Out 【二分+排序】

    任意门:http://codeforces.com/contest/1077/problem/D D. Cutting Out time limit per test 3 seconds memory ...

  4. CodeForces Round #521 (Div.3) D. Cutting Out

    http://codeforces.com/contest/1077/problem/D You are given an array ss consisting of nn integers. Yo ...

  5. CodeForces Round #521 (Div.3) B. Disturbed People

    http://codeforces.com/contest/1077/problem/B There is a house with nn flats situated on the main str ...

  6. Codeforces Round #555 (Div. 3) E. Minimum Array 【数据结构 + 贪心】

    一 题面 E. Minimum Array 二 分析 注意前提条件:$0 \le  a_{i} \lt n$ 并且 $0 \le  b_{i} \lt n$.那么,我们可以在$a_{i}$中任取一个数 ...

  7. CodeForces Round #521 (Div.3) E. Thematic Contests

    http://codeforces.com/contest/1077/problem/E output standard output Polycarp has prepared nn competi ...

  8. Codeforces Round #521 (Div. 3) F1. Pictures with Kittens (easy version)

    F1. Pictures with Kittens (easy version) 题目链接:https://codeforces.com/contest/1077/problem/F1 题意: 给出n ...

  9. CodeForces Round #521 (Div.3) A. Frog Jumping

    http://codeforces.com/contest/1077/problem/A A frog is currently at the point 00 on a coordinate axi ...

随机推荐

  1. 问题:oracle字符串函数;结果:Oracle字符串函数

    Oracle字符串函数 最近换了新公司,又用回Oracle数据库了,很多东西都忘记了,只是有个印象,这两晚抽了点时间,把oracle对字符串的一些处理函数做了一下整理,供日后查看.. 平常我们用Ora ...

  2. elasticsearch 概念与架构(3)

    转自:https://devops.taobao.com/ Node(节点):单个的装有Elasticsearch服务并且提供故障转移和扩展的服务器. Cluster(集群):一个集群就是由一个或多个 ...

  3. PL/SQL批处理语句(一)BULK COLLECT

    我们知道PL/SQL程序中运行SQL语句是存在开销的,因为SQL语句是要提交给SQL引擎处理,这种在PL/SQL引擎和SQL引擎之间的控制转移叫做上下文却换,每次却换时,都有额外的开销.然而,FORA ...

  4. windows下单机版的伪分布式solrCloud环境搭建Tomcat+solr+zookeeper

    原文出自:http://sbp810050504.blog.51cto.com/2799422/1408322           按照该方法,伪分布式solr部署成功                 ...

  5. 洛谷P3328(bzoj 4085)毒瘤线段树

    题面及大致思路:https://www.cnblogs.com/Yangrui-Blog/p/9623294.html, https://www.cnblogs.com/New-Godess/p/45 ...

  6. Codeforces #505(div1+div2) B Weakened Common Divisor

    题意:给你若干个数对,每个数对中可以选择一个个元素,问是否存在一种选择,使得这些数的GCD大于1? 思路:可以把每个数对的元素乘起来,然后求gcd,这样可以直接把所有元素中可能的GCD求出来,从小到大 ...

  7. jQuery实现按钮5秒后可以点击

    废话少说,直接上代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> & ...

  8. BridgePattern(23种设计模式之一)

    生活中的一个例子 就拿汽车在路上行驶的来说.即有小汽车又有公共汽车,它们都不但能在市区中的公路上行驶,也能在高速公路上行驶.这你会发现,对于交通工具(汽车)有不同的类型,然而它们所行驶的环境(路)也在 ...

  9. JavaPersistenceWithHibernate第二版笔记-第七章-003Mapping an identifier bag(@OrderColumn、@ElementCollection、@CollectionTable、、)

    一.结构 二.代码 1. package org.jpwh.model.collections.listofstrings; import org.jpwh.model.Constants; impo ...

  10. 10.model/view实例(4)

    任务:给表单的每一列添加列名. 思考: 1.只需要添加一个函数 headerData(). 横向方面添加列名 代码如下: QVariant MyModel::headerData(int sectio ...