์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Python

[BOJ/Step14] 14889 : ์Šคํƒ€ํŠธ์™€ ๋งํฌ (Python)

NaNaRin๐Ÿ™ƒ 2021. 2. 22. 20:49

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


์‰ฝ๊ฒŒ ํ’€์—ˆ๋Š”๋ฐ ๋ฐ”๋ณด๊ฐ™์€ ๋ถ€๋ถ„ ๋•Œ๋ฌธ์— ํ‹€๋ ค์„œ ํ•œ์ฐธ ๋จธ๋ฆฌ ์‹ธ๋งด....

๋งˆ์ง€๋ง‰์— gap ๋ฆฌ์ŠคํŠธ ๊ตฌํ• ๋•Œ ๋ฒ”์œ„ ์„ค์ •์„ ๋ฉ์ฒญํ•˜๊ฒŒ ํ•ด์„œ ใ…กใ…œใ…  ์–ดํœด ๋ฉ์ฒญ์ด ๋‹ต๋‹ต์•„

 

ํŒ€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๋ฉด ๊ทธ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ t_status()

  1. ์ด์ค‘ ๋ฃจํ”„ ์‚ฌ์šฉ

    1-1. ์ „๋‹ฌ๋œ ํŒ€ ๋ฆฌ์ŠคํŠธ์˜ ํŒ€์› i

    1-2. 1-1์˜ ํŒ€์› i์™€ ๋‹ค๋ฅธ ํŒ€์› j ์˜ ์ ์ˆ˜ Sij

    1-3. Sij๋ฅผ total์— ๋”ํ•ด์ค€๋‹ค

  2. ์ตœ์ข… total ๋ฐ˜ํ™˜

 

1. n์„ ์ž…๋ ฅ๋ฐ›๊ณ  1~n๊นŒ์ง€์˜ ์ˆ˜๋ฅผ ์ €์žฅํ•œ p ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ

2. ๋Šฅ๋ ฅ์น˜๋ฅผ ์ด์ค‘ ๋ฆฌ์ŠคํŠธ status ์— ์ €์žฅ

3. p๋ฆฌ์ŠคํŠธ์˜ ์ธ์› ์ค‘ n//2 ๋งŒํผ์˜ ์ธ์›์„ ๋ฝ‘์„ ์ˆ˜ ์žˆ๋Š” ์กฐํ•ฉ์„ ๋ฆฌ์ŠคํŠธ team ์— ์ €์žฅ => itertools ๋ชจ๋“ˆ์˜ combinations() ํ•จ์ˆ˜ ์‚ฌ์šฉ

4. ๊ฐ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜๋ฅผ t_status() ํ•จ์ˆ˜๋กœ ๊ตฌํ•ด ๋ฆฌ์ŠคํŠธ team_status ์— ์ €์žฅ

5. ๋ฆฌ์ŠคํŠธ team_status์˜ [0]๊ณผ [n-1]์˜ ์ฐจ, [1]๊ณผ [n-2]์˜ ์ฐจ, …, [(n/2)-1], [n/2]์˜ ์ฐจ๋ฅผ ๋ฆฌ์ŠคํŠธ team_status_gap ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ

6. team_status_gap์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅ 

 

=> ์Šคํƒ€ํŠธ ํŒ€๊ณผ ๋งํฌ ํŒ€์„ ๊ฐ๊ฐ ๋‚˜๋ˆ„์ง€ ์•Š์€ ์ด์œ 

    team[0] ๊ณผ team[n-1] ์€ ์ง์ด๋‹ค. team[0] + team[n-1] ์ด ์˜จ์ „ํ•œ team์ด ๋จ

= ์Šคํƒ€ํŠธ ํŒ€์ด team[0] ์กฐํ•ฉ์ด๋ฉด ๋งํฌ ํŒ€์€ ๋ฐ˜๋“œ์‹œ team[n-1] ์กฐํ•ฉ์ด ๋œ๋‹ค

= ๋‘ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜ ์ฐจ์ด์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ด๊ธฐ ๋•Œ๋ฌธ์— team[0] ๊ณผ team[n-1] ์˜ ์ฐจ์ด๋ฅผ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค

 

# 14889.py

import itertools


def t_status(t):
    total = 0
    for i in t:
        for j in t:
            s = status[i-1][j-1]
            total += s
    return total


n = int(input())
p = [i for i in range(1, n+1)]
status = [[i for i in map(int, input().split())] for _ in range(n)]

team = list(itertools.combinations(p, n//2))

team_status = [t_status(i) for i in team]
team_status_gap = [abs(team_status[i] - team_status[-1-i]) for i in range(len(team)//2)]

print(min(team_status_gap))