No Rules Rules

두 용액 (feat. 백준, 2470번) 본문

생활/코테

두 용액 (feat. 백준, 2470번)

개발하는 완두콩 2022. 8. 23. 18:58
728x90
반응형

두 용액
https://www.acmicpc.net/problem/2470

 

2470번: 두 용액

첫째 줄에는 전체 용액의 수 N이 입력된다. N은 2 이상 100,000 이하이다. 둘째 줄에는 용액의 특성값을 나타내는 N개의 정수가 빈칸을 사이에 두고 주어진다. 이 수들은 모두 -1,000,000,000 이상 1,000,00

www.acmicpc.net

 

반응형

 

// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
#include <algorithm>
using namespace std;
int N;
long long arr[100000];
int main() {
	ios::sync_with_stdio(false), cin.tie(NULL);
	cin >> N;
	for (register int n = 0; n < N; ++n)
		cin >> arr[n];
	sort(arr, arr + N);
	register int s = 0, e = N - 1, as, ae;
	register long long minnn = 2000000001;
	while (1) {
		if (s >= e)
			break;
		if (arr[s] + arr[e] == 0) {
			as = s, ae = e;
			break;
		}
		else {
			if (arr[s] + arr[e] < 0) {
				if (abs(arr[s] + arr[e]) < minnn)
					minnn = abs(arr[s] + arr[e]), as = s, ae = e;
				++s;
			}
			else {
				if (abs(arr[s] + arr[e]) < minnn)
					minnn = abs(arr[s] + arr[e]), as = s, ae = e;
				--e;
			}
		}
	}
	cout << arr[as] << " " << arr[ae];
	return 0;
}
// *&)*@*
  1. 투포인터 알고리즘은 두개의 인덱스 (start, end 또는 low, high) 로 진행됩니다.
  2. 앞과 뒤의 인덱스가 서로 반전될때까지 비교하는 방식입니다.

 

728x90
반응형

'생활 > 코테' 카테고리의 다른 글

소수의 연속합 (feat. 백준, 1644번)  (0) 2022.08.24
부분합 (feat. 백준, 1806번)  (0) 2022.08.24
두 수의 합 (feat. 백준, 3273번)  (0) 2022.08.23
운동 (feat. 백준, 1956번)  (0) 2022.08.23
플로이드 (feat. 백준, 11404번)  (0) 2022.08.23
Comments