슈콩

[SWEA] 사랑의 카운슬러 본문

Algorithms/SWEA

[SWEA] 사랑의 카운슬러

shukong 2025. 11. 15. 14:58

 

 

 

[문제]

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWlQUD2qtysDFAVS

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

 

 

[소스 코드]

import java.io.*;
import java.util.*;
public class Solution {
	static int n;
	static long result;
	static long[][] info;
	static boolean[] visit;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int T = Integer.parseInt(br.readLine());
		for(int tc=1;tc<=T;tc++) {
			n = Integer.parseInt(br.readLine());
			info = new long[n][2];
			for(int i=0;i<n;i++) {
				st = new StringTokenizer(br.readLine());
				info[i][0] = Long.parseLong(st.nextToken());
				info[i][1] = Long.parseLong(st.nextToken());
			}
			visit = new boolean[n];
			result = Long.MAX_VALUE;
			select(0,0);
			System.out.println("#"+tc+" "+result);
		}
	}
	private static void select(int idx,int cnt) {
		if(cnt==n/2) {
			long nx = 0;
			long ny = 0;
			for(int i=0;i<n;i++) {
				if(visit[i]) {
					nx += info[i][0];
					ny += info[i][1];
				}
				else {
					nx -= info[i][0];
					ny -= info[i][1];
				}
			}
			long sum = nx*nx + ny*ny;
			result = Math.min(result, sum);
			return;
		}
		for(int i=idx;i<n;i++) {
			visit[i] = true;
			select(i+1,cnt+1);
			visit[i] = false;
		}
	}
}

'Algorithms > SWEA' 카테고리의 다른 글

[SWEA] 화섭이의 정수 나열  (0) 2025.11.15
[SWEA] 공평한 분배 2  (0) 2025.11.15
[SWEA] 신뢰  (0) 2025.11.15
[SWEA] 알파벳 공부  (0) 2025.11.14
[SWEA] 힙  (0) 2025.11.14