문제링크
https://www.acmicpc.net/problem/2470
package BOJ;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
//https://www.acmicpc.net/problem/2470 두용액
public class Main_2470 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int arr[] = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0;i<N;i++) arr[i] = Integer.parseInt(st.nextToken());
Arrays.sort(arr);
//System.out.println(Arrays.toString(arr));
int start = 0;
int end = N-1;
int sum = arr[start] + arr[end];
int temp = sum;
int num1 = arr[start],num2 =arr[end];
while(start<end){
if(sum == 0) break;
if(sum >0){
//오른쪽에꺼 하나 지워주기
sum = sum - arr[end--]+arr[end];
if(start == end) break;
if(Math.abs(temp) > Math.abs(sum)){
temp = sum ;
num1 = arr[start];
num2 = arr[end];
}
}
else if(sum<0){
sum = sum - arr[start++] + arr[start];
if(start == end) break;
if(Math.abs(temp)>Math.abs(sum)){
temp = sum;
num1 = arr[start];
num2 = arr[end];
}
}
}
System.out.println(num1+" "+num2);
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[JAVA]백준_7662_이중 우선순위 큐 (0) | 2021.04.18 |
---|---|
[JAVA]백준_2531_회전초밥 (0) | 2021.04.16 |
[JAVA]백준_17836_ 공주님을구해라 (0) | 2021.04.14 |
[JAVA]백준_1806_부분합 (0) | 2021.04.14 |
[JAVA]백준_2564_경비원 (0) | 2021.04.14 |