알고리즘

[알고리즘] 백준 2473 - 세 용액 (C++)

blueberrysoda 2025. 1. 16. 21:23

[문제 링크] : https://www.acmicpc.net/problem/2473

#include <iostream>
#include <algorithm>
using namespace std;

long long N;
long long Arr[5004];
long long Ans[4];
long long maxi = 1e18; 

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    cin >> N;
    for(int i=1; i<=N; i++){
        cin >> Arr[i];
    }

    sort(Arr+1, Arr+1+N);
    
    for(int i=1; i<=N-2; i++){
        long long start = i+1;
        long long end = N;
        while(start < end){
            long long tmp = Arr[i] + Arr[start] + Arr[end];
            if(abs(tmp) < maxi){
                maxi = abs(tmp);
                Ans[1] = Arr[i];
                Ans[2] = Arr[start];
                Ans[3] = Arr[end];
            }
            if(tmp < 0){
                start++;
            }
            else{
                end--;
            }
        }
    }

    cout << Ans[1] << " " << Ans[2] << " " << Ans[3] << "\n";
    return 0;
}