https://www.acmicpc.net/problem/14889

 

14889번: 스타트와 링크

예제 2의 경우에 (1, 3, 6), (2, 4, 5)로 팀을 나누면 되고, 예제 3의 경우에는 (1, 2, 4, 5), (3, 6, 7, 8)로 팀을 나누면 된다.

www.acmicpc.net

 

  • 두 팀으로 나누는 조합 생성(p)
  • 각각의 팀이 가지는 능력치 담은 power 배열 0으로 초기화
  • 각 팀마다 2명씩 뽑아서 2명의 팀원의 능력치를 해당 power 배열에 담음
  • 팀은 결국 두 팀만 만들어지므로 power배열의 끝에서부터 안쪽으로 상대팀과의 능력 차 구해주면 됨
  • result에 능력 차이 값을 담고, 마지막에 min(result)로 정답 출력

 

import sys; input = sys.stdin.readline
from itertools import combinations

result = []
n = int(input())
graph = [list(map(int, input().split())) for _ in range(n)]

nums = [i for i in range(0,n)]

p = list(combinations(nums, int(n/2)))
power = [0] * len(p)
for i in range(len(p)):
    p_list = list(combinations(p[i], 2))
    for j in p_list:
        x, y = j
        power[i] += graph[x][y] + graph[y][x]

for i in range(int(len(power)/2)):
    j = len(power)-i-1
    result.append(abs(power[i]-power[j]))

print(min(result))

 

'◦ Algorithm > Python' 카테고리의 다른 글

백준 2xn 타일링 2 DP  (0) 2023.05.24
프로그래머스 두 큐 합 같게 하기  (0) 2023.05.22
백준 퇴사 14501 DP  (0) 2023.05.14
백준 로봇 조종하기 2169 DP  (0) 2023.05.09
프로그래머스 실패율 구현  (0) 2023.04.29

+ Recent posts