알고리즘
백준 14921번 용액 합성하기 C++
영춘권의달인
2023. 1. 27. 12:26
https://www.acmicpc.net/problem/14921
14921번: 용액 합성하기
홍익대 화학연구소는 다양한 용액을 보유하고 있다. 각 용액은 -100,000,000부터 100,000,000사이의 특성 값을 갖는데, 같은 양의 두 용액을 혼합하면, 그 특성값은 두 용액의 특성값의 합이 된다. 당
www.acmicpc.net
투 포인터
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int n;
vector<int> v;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
v = vector<int>(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
int left = 0;
int right = n - 1;
int ans = INT_MAX;
while (left < right) {
int val = v[left] + v[right];
if (val < 0) {
left++;
}
else {
right--;
}
if (abs(ans) > abs(val)) {
ans = val;
}
}
cout << ans;
}