문제 링크입니다: https://www.acmicpc.net/problem/2467
2467번: 용액
첫째 줄에는 전체 용액의 수 N이 입력된다. N은 2 이상 100,000 이하의 정수이다. 둘째 줄에는 용액의 특성값을 나타내는 N개의 정수가 빈칸을 사이에 두고 오름차순으로 입력되며, 이 수들은 모두 -1,000,000,000 이상 1,000,000,000 이하이다. N개의 용액들의 특성값은 모두 서로 다르고, 산성 용액만으로나 알칼리성 용액만으로 입력이 주어지는 경우도 있을 수 있다.
www.acmicpc.net
예전에 비슷한 문제를 풀었던 기억이 있습니다.
투 포인터 알고리즘을 통해 풀면 시간 안에 풀 수 있는 문제였습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
int N; | |
cin >> N; | |
vector<int> v(N); | |
for (int i = 0; i < N; i++) | |
{ | |
cin >> v[i]; | |
} | |
sort(v.begin(), v.end()); | |
int i = 0, j = v.size() - 1; | |
int total = 2e9; | |
pair<int, int> result; | |
while (i < j) | |
{ | |
int a = v[i]; | |
int b = v[j]; | |
if (abs(a + b) < total) | |
{ | |
total = abs(a + b); | |
result.first = v[i]; | |
result.second = v[j]; | |
} | |
if (a + b < 0) | |
{ | |
i++; | |
} | |
else | |
{ | |
j--; | |
} | |
} | |
cout << result.first << " " << result.second << "\n"; | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 2501번 약수 구하기 (0) | 2019.10.21 |
---|---|
백준 1253번 좋다 (2) | 2019.10.21 |
백준 7785번 회사에 있는 사람 (0) | 2019.10.21 |
백준 6087번 레이저 통신 (0) | 2019.10.13 |
백준 1981번 배열에서 이동 (2) | 2019.10.08 |