[백준]7453_합이 0인 네 정수

🐈 JAELEE 🐈·2021년 9월 28일
0

https://www.acmicpc.net/problem/7453

Solved

✔ 정렬, 이분탐색, 두 포인터, 중간에서 만나기
[백준]2143_두 배열의 합처럼 만들 수 있는 합의 조합을 두 그룹으로 만들고 이분탐색을 한다. 두 배열의 합과 차이점은 이 문제는 배열이 4개라는 점. 4개라도 똑같이 두 그룹으로 분류하면 된다.

#include <iostream>
#include <vector>
#include <algorithm> 
using namespace std;
typedef long long LL;

int n;
vector<int> a, b, c, d;
vector<int> ab, cd;

LL st2_2() {
    LL res = 0, cnt = 0;
    sort(ab.begin(), ab.end());
    sort(cd.begin(), cd.end());
    int p_cd = cd.size() - 1;
    for (int p_ab = 0; p_ab < ab.size(); p_ab++) {
        int target = -ab[p_ab];
        if (0 < p_ab && ab[p_ab] == ab[p_ab - 1]) {
            res += cnt;
        }
        else {
            while (0 <= p_cd && target < cd[p_cd]) {
                p_cd--;
            }
            cnt = 0;
            while (0 <= p_cd && target == cd[p_cd]) {
                cnt++;
                p_cd--;
            }
            res += cnt;
        }
    }
    return res;
}

LL st1_2() {
    LL res = 0, cnt = 0;
    sort(ab.begin(), ab.end());
    sort(cd.begin(), cd.end());
    for (int i = 0 ; i < ab.size() ; i++) {
        int b_pre = lower_bound(cd.begin(), cd.end(), -ab[i]) - cd.begin();
        int b_end = upper_bound(cd.begin(), cd.end(), -ab[i]) - cd.begin();
        res += (b_end - b_pre);
    }
    return res;
}

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

    cin >> n;
    for (int i = 0; i < n; i++) {
        int u, v, w, x;
        cin >> u >> v >> w >> x;
        a.push_back(u);
        b.push_back(v);
        c.push_back(w);
        d.push_back(x);
    }
    // a,b 로 만들수 있는 합의 조합 ==> ab
    // c,d로 만들수 있는 합의 조합 ==> cd
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            ab.push_back(a[i] + b[j]);
            cd.push_back(c[i] + d[j]);
        }
    }

    //printf("%lld", st_1());
    //printf("%lld", st_2_1());
    cout << st2_2() << '\n';
    return 0;
}

0개의 댓글