백준(BOJ)_용액(2467번)_C++

2023. 3. 1. 06:11Problem Solving/Search & Two Pointer

728x90
반응형
SMALL

기본적인 이분 탐색 문제다.

이미 정렬된 상태로 용액의 특성값이 주어지기 때문에 있는 그대로 이분 탐색을 진행하면 된다.

 

#include <iostream>

using namespace std;

int n, val[100000];
int sol1, sol2, min_value = 2e9;

int main(){
    cin.tie(NULL);
    cout.tie(NULL);
    ios_base::sync_with_stdio(false);

    //input
    cin >> n;
    for(int i = 0; i < n; i++)
        cin >> val[i];
    
    //binary search
    int start = 0;
    int end = n - 1;
    while (start < end){
        int temp = val[start] + val[end];

        if (abs(temp) < min_value){
            sol1 = val[start];
            sol2 = val[end];
            min_value = abs(temp);
        }

        if (temp > 0)
            end--;
        else if (temp < 0)
            start++;
        else if (temp == 0)
            break;
    }

    //output
    cout << sol1 << " " << sol2;
}
728x90
반응형
LIST