1일1알

백준 1253번 좋다 C++ 본문

알고리즘

백준 1253번 좋다 C++

영춘권의달인 2022. 9. 9. 13:23

출처 : https://www.acmicpc.net/problem/1253

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>

using namespace std;
using int64 = long long;

int n;
vector<int> v;
set<int> s;
multimap<int, pair<int,int>> mm;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> n;
    v = vector<int>(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i];
        s.insert(v[i]);
    }
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            int sum = v[i] + v[j];
            if (s.find(sum) == s.end()) continue;
            mm.insert({ sum,{i,j} });
        }
    }
   int ans = 0;
    for (int i = 0; i < n; i++) {
        auto it = mm.find(v[i]);
        while (true) {
            if (it == mm.end() || it->first != v[i]) break;
            if (it->second.first != i && it->second.second != i) {
                ans++;
                break;
            }
            ++it;
        }
    }

    cout << ans;
    
}