백준
백준 소스코드 [C++] 2470 두 용액
Hani_Levenshtein
2021. 3. 2. 06:36
2470번: 두 용액
첫째 줄에는 전체 용액의 수 N이 입력된다. N은 2 이상 100,000 이하이다. 둘째 줄에는 용액의 특성값을 나타내는 N개의 정수가 빈칸을 사이에 두고 주어진다. 이 수들은 모두 -1,000,000,000 이상 1,000,00
www.acmicpc.net
백준 소스코드 [C++] 2470 두 용액
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#include <set>
#define all(v) v.begin(), v.end()
#define pii pair<int,int>
#define make_unique(v) v.erase(unique(v.begin(), v.end()), v.end())
typedef long long ll;
using namespace std;
int n;
vector<int> v;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n; v.resize(n);
for (int i = 0;i < n;i++) cin >> v[i];
sort(all(v));
int L = 0, R = n-1, here=abs(v[L]+v[R]),there;
int l = 0, r = n-1;
while (L<R) {
there = v[L] + v[R];
if (here > abs(there)) {
here = abs(there);
l = L, r = R;
}
if (there < 0) L++;
else R--;
}
cout << v[l] << " " << v[r];
return 0;
}