728x90
https://www.acmicpc.net/problem/2470
// 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
import java.util.*;
import java.io.*;
public class Main{
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static int N;
static int[] arr;
static int pick1 = 0;
static int pick2 = 0;
// value와 같거나 가장 근사한 값을 찾는다.
static void binarySearch() {
int left = 0;
int right = N-1;
int max = 2000000000;
while(left < right) {
int sum = arr[left] + arr[right];
// 두 용액 갱신
if(Math.abs(sum) < max) {
pick1 = arr[left];
pick2 = arr[right];
max = Math.abs(sum);
}
if(sum > 0)
right--;
else
left++;
}
}
public static void main(String[] args) throws Exception {
// Please Enter Your Code Here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
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);
binarySearch();
bw.write(pick1 + " " + pick2);
br.close();
bw.flush();
bw.close();
}
}
|
cs |
반응형