백준
백준 소스코드 [C++] 2473 세 용액
Hani_Levenshtein
2021. 3. 2. 06:50
2473번: 세 용액
첫째 줄에는 전체 용액의 수 N이 입력된다. N은 3 이상 5,000 이하의 정수이다. 둘째 줄에는 용액의 특성값을 나타내는 N개의 정수가 빈칸을 사이에 두고 주어진다. 이 수들은 모두 -1,000,000,000 이상
www.acmicpc.net
백준 소스코드 [C++] 2473 세 용액
#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<ll> 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, M = 1, R = n - 1;
ll here = abs(v[L] + v[M] + v[R]), there;
int l = 0, m = 1, r = n - 1;
for (L = 0;L <= n - 3;L++) {
M = L + 1;
R = n - 1;
while (M < R) {
there = v[L] + v[M] + v[R];
if (here > abs(there)) {
here = abs(there);
l = L, m = M, r = R;
}
if (there < 0) {
M++;
}
else R--;
}
}
cout << v[l] << " " << v[m] << " " << v[r];
return 0;
}