(cpp) Baekjoon 2470번 문제 ‘두 용액’ - 정렬, 투 포인터, 이분탐색
풀이
코드
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n;
long long int f;
vector<int> flist;
vector<int> answer(2);
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> f;
flist.push_back(f);
}
sort(flist.begin(), flist.end());
int l = 0;
int r = n-1;
int min = 2000000001;
while (l < r) {
int result = flist[l] + flist[r];
if (abs(result) < min) {
answer[0] = flist[l];
answer[1] = flist[r];
min = abs(result);
}
if (result < 0) {
l++;
}
else {
r--;
}
}
cout << answer[0] << ' '<< answer[1];
}