# Link

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

 

2473번: 세 용액

첫째 줄에는 전체 용액의 수 N이 입력된다. N은 3 이상 5,000 이하의 정수이다. 둘째 줄에는 용액의 특성값을 나타내는 N개의 정수가 빈칸을 사이에 두고 주어진다. 이 수들은 모두 -1,000,000,000 이상

www.acmicpc.net

 

 

# Code - C++

//
//  BOJ
//  ver.C++
//
//  Created by GGlifer
//
//  Open Source

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

using namespace std;

#define endl '\n'
#define len(x) (int)(x).length()
#define size(x) (int)(x).size()
#define str(x) to_string(x)
#define error logic_error("line " + str(__LINE__))

// Set up : Definitions
/* None */

// Set up : Global Variables
/* None */

// Set up : Functions Declaration
/* None */


int main()
{
    // Set up : I/O
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // Set up : Input
    int N; cin >> N;
    long V[N];
    for (int i=0; i<N; i++)
        cin >> V[i];

    // Process
    sort(V, V+N);

    vector<long> A = {V[0], V[1], V[2]};
    long ans_val = V[0] + V[1] + V[2];

    for (int i=0; i<N-2; i++) {
        int s = i+1, e = N-1;
        while (s < e) {
            long tmp_val = V[i] + V[s] + V[e];
            if (abs(ans_val) > abs(tmp_val)) {
                A = {V[i], V[s], V[e]};
                ans_val = tmp_val;

                if (ans_val == 0) {
                    goto end;
                }
            }

            (tmp_val > 0) ? e-- : s++;
        }
    } end:

    // Control : Output
    cout << A[0] << ' ' << A[1] << ' ' << A[2] << endl;
}

// Helper Functions
/* None */

 

 

지적, 조언, 첨언 모두 환영합니다!!!

+ Recent posts