๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 1253๋ฒˆ - ์ข‹๋‹ค

by dkswnkk 2021. 11. 2.
 

1253๋ฒˆ: ์ข‹๋‹ค

์ฒซ์งธ ์ค„์—๋Š” ์ˆ˜์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 2,000), ๋‘ ๋ฒˆ์งธ ์ค„์—๋Š” i๋ฒˆ์งธ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” Ai๊ฐ€ N๊ฐœ ์ฃผ์–ด์ง„๋‹ค. (|Ai| ≤ 1,000,000,000, Ai๋Š” ์ •์ˆ˜)

www.acmicpc.net

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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

    int N; cin >> N;
    vector<int>v(N);

    for (int i = 0; i < N; i++) {
        cin >> v[i];
    }
    sort(v.begin(), v.end());    //์ด๋ถ„ํƒ์ƒ‰์„ ์œ„ํ•œ ์ •๋ ฌ
    int cnt = 0;
    for (int i = 0; i < N; i++) {
        int start = 0;
        int end = N - 1;
        while (start < end) {
            if (v[start] + v[end] == v[i]) {
                if (start != i && end != i) {
                    cnt++;
                    break;
                }
                else if (start == i) start++;
                else if (end== i) end--;

            }
            else if (v[start] + v[end] < v[i]) start++;
            else end--;
        }
    }

    cout << cnt;

}

๋Œ“๊ธ€