CodeForces - 1077C

CodeForces - 1077C
在这里插入图片描述

在这里插入图片描述

谷歌翻译:
如果数组中的元素等于所有其他元素的总和,那么让我们调用数组。例如,数组a = [1,3,3,7]是好的,因为元素a4 = 7等于1 + 3 + 3的和。你给出一个由n个整数组成的数组。你的任务是打印这个数组的所有索引j,以便删除j后数组中的第 - 个元素会很好(让我们调用这样的索引很好)。例如,如果a = [8,3,5,2],那么漂亮的索引是1和4:如果删除a1,则数组看起来像[3,5,2]并且它是好的;如果你删除a4,数组将看起来像[8,3,5]并且很好。你必须独立考虑所有删除,i。即删除元素,检查结果数组是否正常,并将元素返回到数组中。
Input
输入的第一行包含一个整数n(2≤n≤2⋅105) - 数组a中元素的数量。输入的第二行包含n个整数a1,a2,…,an(1≤ai≤ 106) - 数组a的元素。
Output
在第一行中打印一个整数k - 数组a的索引j的数量
这样,在从数组中删除第j个元素之后,它将是好的(即打印好的索引的数量)。在第二行打印k个不同的整数j1,j2,…,jk以任何顺序排列 - 数组的漂亮索引a。如果数组a中没有这样的索引,只需在第一行打印0并将第二行留空或根本不打印。

思路:

AC代码:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
#define LL long long
#define ll LLconst int MAX = 2e6 + 7;

using namespace std;

struct aa {
    int th;
    long long num;
} a[200005];
int cmp(aa x, aa y){
    return x.num < y.num;
}vector<int> bb;
int main(void){
    int n;  
    long long sum = 0;    
    cin >> n;    
    for (int i = 1; i <= n; i++) {
        cin >> a[i].num;
        a[i].th = i;        
        sum += a[i].num;    
    }    
    sort(a + 1, a + 1 + n, cmp);    
    for (int i = 1; i <= n; i++) {
        if (i != n) {            
            if (sum - a[n].num - a[i].num == a[n].num)          
            bb.push_back(a[i].th);       
        }         
        else {            
            if (sum - a[n - 1].num - a[i].num == a[n - 1].num)               
                bb.push_back(a[i].th);       
            }    
   }    
   if (bb.size()) {
       cout << bb.size() << endl;        
       for (int i = 0; i < bb.size(); i++)            
           cout << bb[i] << " ";   
       } 
   else        
       cout << "0";
   return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值