zoukankan      html  css  js  c++  java
  • CodeForces Round #521 (Div.3) C. Good Array

    http://codeforces.com/contest/1077/problem/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 (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

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    const int maxn = 2e5 + 10;
    int n;
    long long sum = 0;
    
    struct Node{
        int pos;
        int val;
    }node[maxn];
    
    map<long long, int> mp;
    
    int main() {
        scanf("%d", &n);
        mp.clear();
        for(int i = 1; i <= n; i ++) {
            int x;
            scanf("%d", &x);
            sum += x;
            node[i].pos = i;
            node[i].val = x;
            mp[x] ++;
        }
    
        vector<int> v;
        for(int i = 1; i <= n; i ++) {
            int temp = node[i].pos;
            mp[node[i].val] --;
            sum -= node[i].val;
    
            if(sum % 2 == 0 && mp[sum / 2])
                v.push_back(temp);
    
            sum += node[i].val;
            mp[node[i].val] ++;
        }
    
        printf("%d
    ", v.size());
        for(int i = 0; i < v.size(); i ++) {
            printf("%d", v[i]);
            printf("%s", i != v.size() - 1 ? " " : "
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    Linux0.11之初识Makefile/build.c
    主机(windows10)虚拟机(ubuntu18)arm板(linux3.4)相互ping通
    windows上利用dhcpsrv搭建DHCP服务器
    《剑指offer》面试题27 二叉搜索树与双向链表 Java版
    《剑指offer》面试题26 复杂链表的复制 Java版
    《剑指offer》面试题25 二叉树中和为某一值的路径 Java版
    《剑指offer》面试题24 二叉搜索树的后序遍历序列 Java版
    异常处理
    三元表达式、列表推导式、生成器表达式、匿名函数、内置函数、递归调用与二分法的简单认识
    迭代器与生成器
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/9991164.html
Copyright © 2011-2022 走看看