300x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 코딩
- dp
- 백준
- nlp
- linux
- 프로그래밍
- 구글 킥스타트
- google coding competition
- 순열
- CSS
- 딥러닝
- 동적 프로그래밍
- 코딩 테스트
- 파이썬
- 운영체제
- 킥스타트
- 브루트포스
- 알고리즘
- BFS
- DFS
- OS
- 그래프
- 프로그래머스
- 네트워크
- 리눅스
- kick start
- 코딩테스트
- 동적프로그래밍
- PYTHON
- AI
Archives
- Today
- Total
오뚝이개발자
[백준14889] 스타트링크 본문
728x90
300x250
문제
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
생각의 흐름
combinations() 메소드를 이용하여 team1의 조합을 구한 뒤 그에 대응되는 team2의 조합을 구해 각 team의 구성원들의 능력치합 sum1, sum2를 구해 차이를 비교해주면 된다. 단순히 "차이"이므로 abs()를 써주어야 한다.
코드
# BOJ 14889
import itertools
import sys
n = int(input())
mat = [list(map(int, input().split())) for _ in range(n)]
member = [i for i in range(n)]
gap = sys.maxsize
tt = list(itertools.combinations(member, n//2))
for team1 in tt:
sum1, sum2 = 0, 0
team2 = [x for x in member if x not in team1]
for x, y in (itertools.combinations(team1, 2)):
sum1 += mat[x][y] + mat[y][x]
for x, y in (itertools.combinations(team2, 2)):
sum2 += mat[x][y] + mat[y][x]
gap = min(gap, abs(sum1-sum2))
print(gap)
728x90
300x250
'코딩 테스트 > 백준' 카테고리의 다른 글
[백준1339] 단어 수학 (0) | 2020.05.01 |
---|---|
[백준1644] 소수의 연속합 (0) | 2020.05.01 |
[백준15990] 1, 2, 3 더하기 5 (0) | 2020.04.28 |
[백준15988] 1, 2, 3 더하기 3 (0) | 2020.04.28 |
[백준1699] 제곱수의 합 (0) | 2020.04.28 |
Comments