[2470] 두 용액

yyeahh·2021년 3월 16일
0

Baekjoon

목록 보기
8/19

[2470] 두 용액

[2021.03.16]
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

int main() {
    int n, t, answer = 2000000005;

    scanf("%d",& n);
    vector<int> num(n);
    for(int i = 0; i < n; i++)
        scanf("%d", &num[i]);
    sort(num.begin(), num.end());
    
    int l = 0, r = n - 1, ln, rn;
    while(l < r) {
        int t = abs(num[l] + num[r]);
        if(t < answer) {
            answer = t;
            ln = l; rn = r;
            if(answer == 0) break;
        }
        (abs(num[l + 1]+num[r]) > abs(num[l]+num[r - 1])) ? r-- : l++;
    }
    printf("%d %d", num[ln], num[rn]);
}

0개의 댓글