|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static int N, half; |
| 7 | + static int[] x; |
| 8 | + static int[] y; |
| 9 | + static long totalX, totalY; |
| 10 | + static double best; |
| 11 | + |
| 12 | + public static void main(String[] args) throws Exception { |
| 13 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 14 | + int T = Integer.parseInt(br.readLine()); |
| 15 | + |
| 16 | + StringBuilder sb = new StringBuilder(); |
| 17 | + for (int t = 0; t < T; t++) { |
| 18 | + N = Integer.parseInt(br.readLine()); |
| 19 | + half = N / 2; |
| 20 | + x = new int[N]; |
| 21 | + y = new int[N]; |
| 22 | + totalX = 0; |
| 23 | + totalY = 0; |
| 24 | + |
| 25 | + for (int i = 0; i < N; i++) { |
| 26 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 27 | + x[i] = Integer.parseInt(st.nextToken()); |
| 28 | + y[i] = Integer.parseInt(st.nextToken()); |
| 29 | + totalX += x[i]; |
| 30 | + totalY += y[i]; |
| 31 | + } |
| 32 | + |
| 33 | + best = Double.MAX_VALUE; |
| 34 | + dfs(0, 0, 0, 0); |
| 35 | + sb.append(String.format("%f\n", best)); |
| 36 | + } |
| 37 | + System.out.print(sb); |
| 38 | + } |
| 39 | + static void dfs(int index, int count, long curX, long curY) { |
| 40 | + if (count == half) { |
| 41 | + // 선택한 점들의 합으로부터 최종 벡터 합 계산: 2*(cur) - (total) |
| 42 | + long diffX = 2 * curX - totalX; |
| 43 | + long diffY = 2 * curY - totalY; |
| 44 | + double length = Math.sqrt(diffX * diffX + diffY * diffY); |
| 45 | + best = Math.min(best, length); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if (index >= N || (N - index) < (half - count))//범위를 넘거나 선택해야 하는 점 개수만큼 남아있지 않을때 |
| 50 | + return; |
| 51 | + |
| 52 | + dfs(index + 1, count + 1, curX + x[index], curY + y[index]);// 현재 점 선택 |
| 53 | + dfs(index + 1, count, curX, curY);// 현재 점 선택x |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +``` |
0 commit comments