|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.StringTokenizer; |
| 5 | + |
| 6 | +public class BJ_2473_세_용액 { |
| 7 | + |
| 8 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 9 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 10 | + private static StringTokenizer st; |
| 11 | + |
| 12 | + private static int N, selected, left, right; |
| 13 | + private static long sum, min; |
| 14 | + private static int[] solutions, idx; |
| 15 | + |
| 16 | + public static void main(String[] args) throws IOException { |
| 17 | + init(); |
| 18 | + sol(); |
| 19 | + |
| 20 | + bw.flush(); |
| 21 | + bw.close(); |
| 22 | + br.close(); |
| 23 | + } |
| 24 | + |
| 25 | + private static void init() throws IOException { |
| 26 | + N = Integer.parseInt(br.readLine()); |
| 27 | + min = Long.MAX_VALUE; |
| 28 | + |
| 29 | + idx = new int[3]; |
| 30 | + solutions = new int[N]; |
| 31 | + st = new StringTokenizer(br.readLine()); |
| 32 | + for (int i = 0; i < N; i++) { |
| 33 | + solutions[i] = Integer.parseInt(st.nextToken()); |
| 34 | + } |
| 35 | + Arrays.sort(solutions); |
| 36 | + } |
| 37 | + |
| 38 | + private static void sol() throws IOException { |
| 39 | + for (int i = 0; i < N - 2; i++) { |
| 40 | + selected = solutions[i]; |
| 41 | + |
| 42 | + left = i + 1; |
| 43 | + right = N - 1; |
| 44 | + while (left < right) { |
| 45 | + sum = (long) solutions[left] + solutions[right] + selected; |
| 46 | + System.out.println(selected + " " + solutions[left] + " " + solutions[right]); |
| 47 | + System.out.println(sum + " " + min); |
| 48 | + |
| 49 | + if (Math.abs(sum) < min) { |
| 50 | + min = Math.abs(sum); |
| 51 | + idx[0] = i; |
| 52 | + idx[1] = left; |
| 53 | + idx[2] = right; |
| 54 | + } |
| 55 | + |
| 56 | + if (sum == 0) { |
| 57 | + bw.write(selected + " " + solutions[left] + " " + solutions[right]); |
| 58 | + return; |
| 59 | + } else if (sum < 0) { |
| 60 | + left++; |
| 61 | + } else { |
| 62 | + right--; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + bw.write(solutions[idx[0]] + " " + solutions[idx[1]] + " " + solutions[idx[2]]); |
| 67 | + } |
| 68 | + |
| 69 | +} |
| 70 | +``` |
0 commit comments