BOJ 2470 두 용액

1 개요[ | ]

BOJ 2470 두 용액


2 C++[ | ]

#include <bits/stdc++.h>
#include <limits.h>
using namespace std;

int N;
int A[100000];
int INF=INT_MAX;

void solve() {
    int ans1, ans2;
    sort(A, A+N);
    int start=0, end=N-1, m=INF;
    while(start < end) {
        int sum = A[start] + A[end];
        if(abs(sum) < m) {
            ans1 = A[start];
            ans2 = A[end];
            m = abs(sum);
        }
        if(sum < 0) {
            start++;
        } else {
            end--;
        }
    }
    cout << ans1 << ' ' << ans2;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> N;
    for(int i=0; i<N; i++) {
        cin >> A[i];
    }
    solve();
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}