-
[백준] 2470 두 용액ALGORITHM/BOJ 2021. 9. 26. 23:57
https://www.acmicpc.net/problem/2470
2021-09-26
1234567891011121314151617181920212223242526272829303132333435363738import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;import java.util.StringTokenizer;public class Main2470 {public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine().trim());int N = Integer.parseInt(st.nextToken());int[] arr = new int[N];st = new StringTokenizer(bf.readLine());for(int i = 0; i < N; i++) {arr[i] = Integer.parseInt(st.nextToken());}Arrays.sort(arr);int start = 0;int end = N - 1;int sum = 2000000000;int min = 0;int max = 0;while(start < end) {int abs = Math.abs(arr[start] + arr[end]);if(abs < sum) {sum = abs;min = arr[start];max = arr[end];}if(arr[start] + arr[end] > 0) end--; // end 값 > start 값 : 합이 커서 줄여야함else start++; // start > end : 합이 작아서 늘려야함}System.out.println(min + " " +max);}}cs #문제풀이
정렬 후 투 포인터
'ALGORITHM > BOJ' 카테고리의 다른 글
[백준] 10816 숫자 카드 2 (0) 2021.09.29 [백준] 1992 쿼드트리 (0) 2021.09.29 [백준] 2075 N번째 큰 수 (0) 2021.09.26 [백준] 10159 저울 (0) 2021.08.31 [백준] 1717 집합의 표현 (0) 2021.08.14