[λ°±μ€] π₯ 2470 λ μ©μ‘
π λμ΄λ
π₯ Gold 5
π λ¬Έμ
https://www.acmicpc.net/problem/2470
π νμ΄
μΌλ¨ μ΄ λ¬Έμ λ₯Ό λ³΄κ³ λ μκ°μ μ λ ¬μ νκΈ°λ ν΄μΌνλλ°, Arrays.Sort
λ₯Ό μ¬μ©νλ©΄ o(n^2) = 10,000,000,000
μ μκ°λ³΅μ‘λκ° μκΈ΄λ€.
λ°λΌμ, o(nlog(n))
μ μκ°λ³΅μ‘λμΈ μ°μ μμ νλ₯Ό ν΅ν΄μ μ λ ¬μ νμλ€.
νμ§λ§ μ°μ μμ νλ₯Ό μ¬μ©νλ©΄ κ°μ₯ ν° κ°μ μ μ μμ§λ§, κ°μ₯ μμ κ°μ μ μκ° μλ€.
κ·Έλμ μ€λ¦μ°¨μ μ°μ μμ νμ, λ΄λ¦Όμ°¨μ μ°μ μμ ν λκ°λ₯Ό λ§λ€μ΄μ, μλ‘ κ°μ΄ κ°μ λκΉμ§ λΉκ΅λ₯Ό ν΄μ£Όμλ€.
λ€μμ κ·Έ μ½λμ΄λ€.
π Code
package BOJ.TwoPointer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class N2470 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
PriorityQueue<Integer> ascend = new PriorityQueue<>();
PriorityQueue<Integer> descend = new PriorityQueue<>(Collections.reverseOrder());
st = new StringTokenizer(br.readLine());
int num;
for (int i = 0; i < N; i++) {
num = Integer.parseInt(st.nextToken());
ascend.add(num);
descend.add(num);
}
int max = descend.remove();
int min = ascend.remove();
int[] closeZero = {max, min};
while (max != min) {
if (max + min > 0) {
max = descend.remove();
} else {
min = ascend.remove();
}
if (max == min) {
break;
}
if (Math.abs(closeZero[0] + closeZero[1]) > Math.abs(max + min)) {
closeZero = new int[]{max, min};
}
}
System.out.println(closeZero[1] + " " + closeZero[0]);
}
}
package (μ΄λ¦); λ₯Ό λκ³ , class μ΄λ¦μ Main
μΌλ‘ λ³κ²½νλ©΄ λλ€.
κ°μΈ κ³΅λΆ κΈ°λ‘μ© λΈλ‘κ·Έμ
λλ€.
ν리거λ μ€λ₯κ° μμ κ²½μ° μ 보ν΄μ£Όμλ©΄ κ°μ¬νκ² μ΅λλ€.π