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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章