티스토리 뷰

백준

백준 소스코드 [C++] 2467 용액

Hani_Levenshtein 2021. 3. 2. 06:23

www.acmicpc.net/problem/2467

 

2467번: 용액

첫째 줄에는 전체 용액의 수 N이 입력된다. N은 2 이상 100,000 이하의 정수이다. 둘째 줄에는 용액의 특성값을 나타내는 N개의 정수가 빈칸을 사이에 두고 오름차순으로 입력되며, 이 수들은 모두 -

www.acmicpc.net

백준 소스코드 [C++] 2467 용액

#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+1);
	for (int i = 1;i <= n;i++) cin >> v[i];
	int L = 1, R = n, here=abs(v[L]+v[R]),there;
	int l = 1, r = n;
	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;
}
댓글