BOJ : https://www.acmicpc.net/problem/2470
이분탐색으로 해결가능하다.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define MAX 2000000001
ll arr[100005];
ll maxx = MAX;
pii ans;
void bound(int s, int e, ll key) {
int m;
while (s <= e) {
m = (s + e) / 2;
ll ret = abs(0 - (key + arr[m]));
if (ret <= maxx) {
maxx = ret;
ans = { arr[m], key };
}
ll left = abs(0 - (key + arr[m-1]));
ll right = abs(0 - (key + arr[m+1]));
if (left <= right) e = m - 1;
else s = m + 1;
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int N; cin >> N;
for (int i = 0; i < N; i++) cin >> arr[i];
sort(arr, arr + N);
for (int i = 0; i < N; i++) {
bound(0, i - 1, arr[i]);
bound(i + 1, N-1, arr[i]);
}
if (ans.first > ans.second) swap(ans.first, ans.second);
cout << ans.first << ' ' << ans.second;
}
'algorithm > BOJ' 카테고리의 다른 글
scpc 2019 예선 1차 2번 문제 : 공 굴리기 - Junnnho (0) | 2019.07.04 |
---|---|
scpc 2019 예선 1차 1번 문제 : 오르락 내리락 - Junnnho (0) | 2019.07.04 |
에라토스테네스의 체 (0) | 2019.06.30 |
BOJ 11729번 하노이 탑 이동 순서 (0) | 2019.05.31 |
백준 1780번 : 종이의 개수 - Junnnho (0) | 2019.05.30 |