题目链接:http://codeforces.com/problemset/problem/1077/C

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 — niceindices 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
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 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.

题意:输入的第一行  为第二行所要输入的整数的数量(数量的范围2~2e5,第二行整数范围1~1e6)

将第二行所给数组去掉一个数 使得其中一个数会等于其余数的和

初步思路1:先算出第二行所给数组的和sum  依次循环数组每一个元素  再用sum减去所遍历到的元素得q=sum-a[i]  如果q不是2的倍数  显然它不满足要求(因为如果去掉a[i]  剩下的元素中如果存在一个元素  可以用其他元素的和表示  那么剩下所有元素的和等于那个元素的两倍)。。。。剩下的工作就是 等到找到求出满足要求的j   再判断它是否和所对应的a[i]的关系(为什么呢?因为我们遍历到的a[i]  用sum减去以后得到q  相当于将a[i]从数组中拿走  即不存在一个下标为i的a[i])所以

(1)如果j==a[i]  说明数组中存在至少两个a[i]  使得去掉遍历的a[i]后仍有元素等于a[i](即j);

(2)如果j!=a[i]  那么要么数值为j的数在a数组中只出现过一次;或者出现过两次但是数组只有三个元素;出现三次就不太可能了  因为不存在有一个数等于剩下的和自己相等数值的  两  个  数  的和。

*ans[a]=b表示   数值为a的元素在a数组中出现了b次

代码如下:

 1 #include<stdio.h>
 2 #include<string.h>
 3 typedef long long LL;
 4 const int maxn=2e5+10;
 5 int t,cnt,a[maxn],flag,ans[1000000+10],b[maxn];
 6 LL sum;
 7 void init()
 8 {
 9     memset(ans,0,sizeof(ans));
10     memset(b,0,sizeof(b));
11     sum=cnt=flag=0;
12 }
13 int main()
14 {
15     init();
16     scanf("%d",&t);
17     for(int i=1;i<=t;i++)
18     {
19         scanf("%d",&a[i]);
20         sum+=a[i];
21         ans[a[i]]++;
22     }
23     for(int i=1;i<=t;i++)
24     {
25         LL q=sum-a[i];
26         LL j=q/2;
27         if(q%2)continue;
28         if(j<=1000000)//这条不能少 少了会test6过不了
29         {
30             //(1)如果j==a[i]  说明数组中存在至少两个a[i]  使得去掉遍历的a[i]后仍有元素等于a[i](即j);
31             //(2)如果j!=a[i]  那么要么数值为j的数在a数组中只出现过一次;或者出现过两次但是数组只有三个元素。
32             if(j==a[i]&&ans[j]>1||ans[j]==1&&a[i]!=j||t==3&&ans[j]==2&&a[i]!=j)
33             {
34                 b[++cnt]=i;flag=1;
35             }
36         }
37     }
38     if(flag)
39     {
40         printf("%d\n",cnt);
41         for(int i=1;i<=cnt;i++)
42         {
43             printf("%d ",b[i]);
44         }
45         printf("\n");
46     }
47     else
48     printf("0\n");
49     return 0;
50  } 

被j的要有范围坑了  wa了好多次  还是不知道为什么要限制题目给定的输入元素的范围。

思路2:

代码如下:

 1 # include <bits/stdc++.h>
 2
 3 # define ll long long
 4
 5 struct p
 6 {
 7     int id;
 8     ll x;
 9 };
10
11 p a[200001];
12
13 int cmp(p x, p y){return x.x < y.x;}
14
15 std::vector<int> vec;
16
17 int main()
18 {
19     int n;
20     ll sum = 0;
21     scanf("%d", &n);
22     for(int i = 1; i <= n; i++)
23         scanf("%d", &a[i].x), a[i].id = i, sum += a[i].x;
24     std::sort(a + 1, a + n + 1, cmp);
25     for(int i = 1; i <= n; i++)
26     {
27         if(i != n)//情况1
28         {
29             if(sum - a[n].x - a[i].x == a[n].x)
30                 vec.push_back(a[i].id);
31         }
32         else //情况2
33         {
34             if (sum - a[n - 1].x - a[i].x == a[n - 1].x)
35                 vec.push_back(a[i].id);
36         }
37     }
38     if(vec.size())
39     {
40         printf("%d\n", vec.size());
41         for(int i = 0; i < vec.size(); i++)
42             printf("%d ", vec[i]);
43     }
44     else
45         printf("0");
46     return 0;
47 }

如有错误请多多指正,谢谢阅读!

转载于:https://www.cnblogs.com/Mingusu/p/11268938.html

CF1077C——Good Array题解相关推荐

  1. 【Codeforces Round #767 (Div. 2)】 C. Meximum Array 题解

    [Codeforces Round #767 (Div. 2) ]C. Meximum Array 题解 1629C: Meximum Array 题解 [Codeforces Round #767 ...

  2. asp子窗口读取父窗口数据_算法与数据结构基础 - 数组(Array)

    数组基础 数组是最基础的数据结构,特点是O(1)时间读取任意下标元素,经常应用于排序(Sort).双指针(Two Pointers).二分查找(Binary Search).动态规划(DP)等算法.顺 ...

  3. 莫队算法(普通莫队、带修莫队、树上莫队、不删除莫队)学习笔记【理解+套路/核心代码+例题及题解】

    一.理解 我的理解就是巧妙的暴力,利用双指针以及分块思想,巧妙的移动双指针,时间复杂度可以达到O(NlogN). 强推博客:写的又好又全.链接 二.套路 1.普通莫队 [1]核心代码 bool cmp ...

  4. Leetcode-169 Majority Element

    #169 Majority Element Given an array of size n, find the majority element. The majority element is t ...

  5. LeetCode String Compression

    原题链接在这里:https://leetcode.com/problems/string-compression/description/ 题目: Given an array of characte ...

  6. 算法与数据结构基础 - 堆(Heap)和优先级队列(Priority Queue)

    堆基础 堆(Heap)是具有这样性质的数据结构:1/完全二叉树 2/所有节点的值大于等于(或小于等于)子节点的值: 图片来源:这里 堆可以用数组存储,插入.删除会触发节点shift_down.shif ...

  7. Codeforces Round #775 (Div. 2, based on Moscow Open Olympiad in Informatics)简训

    Codeforces Round #775 (Div. 2, based on Moscow Open Olympiad in Informatics)简训 导语 涉及的知识点 题目 A Game B ...

  8. 线段树详解 (原理,实现与应用)

    线段树详解 By 岩之痕 目录: 一:综述 二:原理 三:递归实现 四:非递归原理 五:非递归实现 六:线段树解题模型 七:扫描线 八:可持久化 (主席树) 九:练习题 一:综述 假设有编号从1到n的 ...

  9. 【转载】线段树题目2

    1.hdu1166 敌兵布阵 更新节点,区间求和. 2.hdu1754 I Hate It 更新节点,区间最值. . 3.hdu1698 Just a Hook 成段更新,总区间求和. . 4.hdu ...

最新文章

  1. LJL-Solution-vss2005在项目中连接不上 解决方案
  2. 2020年高等数学方法与提高(上海理工大学)学习笔记:一元函数积分学
  3. HTTP Request Content-Type:application/x-www-form-urlencoded、multipart/form-data、application/json
  4. 关于centos docker版本过低导致 is not a valid repository/tag: invalid reference format
  5. c语言分组求和函数,R语言 实现data.frame 分组计数、求和等
  6. matlab寻找向量最小值,matlab – 在排序向量中快速搜索大于x的最小值
  7. sublime-text3 安装 emmet 插件
  8. javascript 点点滴滴01章 javascript的认知
  9. [html] iframe在更改了src之后,不出现后退或者前进按钮怎么解决?
  10. 如果我用你待我的方式来待你 恐怕你早已离去
  11. LFS安装过程记录(1)-准备工作
  12. exists hive中如何使用_07045.16.2如何使用Hive合并小文件
  13. eclipse中输入@符号自动提示Annotation
  14. torch.sort()
  15. 程序员面试如何与HR谈薪
  16. Hive内表和外表浅析
  17. nginx 安装到Java代码上传图片利用ftp过程遇到的问题总结
  18. 学习到第一个国庆的感想
  19. 基于Qt的ui图形化界面进行的界面设计
  20. RTK模块性能测试分析对比-GNSS实测- RTK板卡100赫兹延迟实测及分析

热门文章

  1. 专业兴趣小组的建设实施方案(讨论稿)
  2. 简单理解Socket 协议
  3. 计算机多媒体技术视差估计,立体视频中视差估计研究
  4. JAVA复制文件夹的第二种方法
  5. 【EI会议推荐】第六届先进电子材料、计算机与软件工程国际学术会议(AEMCSE 2023)
  6. 【脑机接口】利用MNE进行EEG数据预处理(SEED数据集)
  7. 专访百度副总裁景鲲:智能音箱进入黄金期,让小度助手像贾维斯一样无处不在...
  8. 《变形金刚》----快!快!!快!!!
  9. 铁矿行业BI经营分析框架(二)万能框架-增长性、盈利性、流动性
  10. 13.包装类型应用及场景