[๋ฐฑ์ค€/python] 14889๋ฒˆ : ์Šคํƒ€ํŠธ์™€ ๋งํฌ

2021. 4. 5. 17:07

๋ฌธ์ œ

์˜ค๋Š˜์€ ์Šคํƒ€ํŠธ๋งํฌ์— ๋‹ค๋‹ˆ๋Š” ์‚ฌ๋žŒ๋“ค์ด ๋ชจ์—ฌ์„œ ์ถ•๊ตฌ๋ฅผ ํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ์ถ•๊ตฌ๋Š” ํ‰์ผ ์˜คํ›„์— ํ•˜๊ณ  ์˜๋ฌด ์ฐธ์„๋„ ์•„๋‹ˆ๋‹ค. ์ถ•๊ตฌ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ์ธ ์‚ฌ๋žŒ์€ ์ด N๋ช…์ด๊ณ  ์‹ ๊ธฐํ•˜๊ฒŒ๋„ N์€ ์ง์ˆ˜์ด๋‹ค. ์ด์ œ N/2๋ช…์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์Šคํƒ€ํŠธ ํŒ€๊ณผ ๋งํฌ ํŒ€์œผ๋กœ ์‚ฌ๋žŒ๋“ค์„ ๋‚˜๋ˆ ์•ผ ํ•œ๋‹ค.

BOJ๋ฅผ ์šด์˜ํ•˜๋Š” ํšŒ์‚ฌ ๋‹ต๊ฒŒ ์‚ฌ๋žŒ์—๊ฒŒ ๋ฒˆํ˜ธ๋ฅผ 1๋ถ€ํ„ฐ N๊นŒ์ง€๋กœ ๋ฐฐ์ •ํ–ˆ๊ณ , ์•„๋ž˜์™€ ๊ฐ™์€ ๋Šฅ๋ ฅ์น˜๋ฅผ ์กฐ์‚ฌํ–ˆ๋‹ค. ๋Šฅ๋ ฅ์น˜ Sij๋Š” i๋ฒˆ ์‚ฌ๋žŒ๊ณผ j๋ฒˆ ์‚ฌ๋žŒ์ด ๊ฐ™์€ ํŒ€์— ์†ํ–ˆ์„ ๋•Œ, ํŒ€์— ๋”ํ•ด์ง€๋Š” ๋Šฅ๋ ฅ์น˜์ด๋‹ค. ํŒ€์˜ ๋Šฅ๋ ฅ์น˜๋Š” ํŒ€์— ์†ํ•œ ๋ชจ๋“  ์Œ์˜ ๋Šฅ๋ ฅ์น˜ Sij์˜ ํ•ฉ์ด๋‹ค. Sij๋Š” Sji์™€ ๋‹ค๋ฅผ ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, i๋ฒˆ ์‚ฌ๋žŒ๊ณผ j๋ฒˆ ์‚ฌ๋žŒ์ด ๊ฐ™์€ ํŒ€์— ์†ํ–ˆ์„ ๋•Œ, ํŒ€์— ๋”ํ•ด์ง€๋Š” ๋Šฅ๋ ฅ์น˜๋Š” Sij์™€ Sji์ด๋‹ค.

N=4์ด๊ณ , S๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ์‚ดํŽด๋ณด์ž.

i/j 1 2 3 4
1   1 2 3
2 4   5 6
3 7 1   2
4 3 4 5  

์˜ˆ๋ฅผ ๋“ค์–ด, 1, 2๋ฒˆ์ด ์Šคํƒ€ํŠธ ํŒ€, 3, 4๋ฒˆ์ด ๋งํฌ ํŒ€์— ์†ํ•œ ๊ฒฝ์šฐ์— ๋‘ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • ์Šคํƒ€ํŠธ ํŒ€: S12 + S21 = 1 + 4 = 5
  • ๋งํฌ ํŒ€: S34 + S43 = 2 + 5 = 7

1, 3๋ฒˆ์ด ์Šคํƒ€ํŠธ ํŒ€, 2, 4๋ฒˆ์ด ๋งํฌ ํŒ€์— ์†ํ•˜๋ฉด, ๋‘ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • ์Šคํƒ€ํŠธ ํŒ€: S13 + S31 = 2 + 7 = 9
  • ๋งํฌ ํŒ€: S24 + S42 = 6 + 4 = 10

์ถ•๊ตฌ๋ฅผ ์žฌ๋ฏธ์žˆ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์Šคํƒ€ํŠธ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜์™€ ๋งํฌ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜์˜ ์ฐจ์ด๋ฅผ ์ตœ์†Œ๋กœ ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์œ„์˜ ์˜ˆ์ œ์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” 1, 4๋ฒˆ์ด ์Šคํƒ€ํŠธ ํŒ€, 2, 3๋ฒˆ ํŒ€์ด ๋งํฌ ํŒ€์— ์†ํ•˜๋ฉด ์Šคํƒ€ํŠธ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜๋Š” 6, ๋งํฌ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜๋Š” 6์ด ๋˜์–ด์„œ ์ฐจ์ด๊ฐ€ 0์ด ๋˜๊ณ  ์ด ๊ฐ’์ด ์ตœ์†Œ์ด๋‹ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N(4 ≤ N ≤ 20, N์€ ์ง์ˆ˜)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ์ค„์€ N๊ฐœ์˜ ์ˆ˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , i๋ฒˆ ์ค„์˜ j๋ฒˆ์งธ ์ˆ˜๋Š” Sij ์ด๋‹ค. Sii๋Š” ํ•ญ์ƒ 0์ด๊ณ , ๋‚˜๋จธ์ง€ Sij๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜์ด๋‹ค.

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์Šคํƒ€ํŠธ ํŒ€๊ณผ ๋งํฌ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜์˜ ์ฐจ์ด์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

์ •๋‹ต

import sys
from itertools import combinations

N = int(sys.stdin.readline())
S = []
for i in range(N):
    S.append(list(map(int, sys.stdin.readline().split())))

all_players = [i for i in range(N)]
teams = []
for t in combinations(all_players, N // 2):
    teams.append(list(t))

answer = []
for t in range(len(teams) // 2):

    A_stats = 0
    for i in teams[t]:
        for j in teams[t]:
            A_stats += S[i][j]

    B_stats = 0
    for i in teams[-(t + 1)]:
        for j in teams[-(t + 1)]:
            B_stats += S[i][j]

    answer.append(abs(A_stats - B_stats))

print(min(answer))

all_players = [i for i in range(N)]
teams = []
for t in combinations(all_players, N // 2):
    teams.append(list(t))

combinations๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€๋Šฅํ•œ ํŒ€ ์กฐํ•ฉ์„ teams์— ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ €์žฅํ•œ๋‹ค.

teams์˜ ์ฒซ๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ๋Š” ๋งˆ์ง€๋ง‰ ๋ฆฌ์ŠคํŠธ์™€ ์ง์ง€์–ด์ง€๋ฏ€๋กœ teams[i]์™€ teams[-(i+1)]์ด ์ง์ง€์–ด์ง„๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด 0, 1, 2, 3์˜ ํŒ€์ด๋ผ๋ฉด,

teams์— [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]] ํ˜•ํƒœ๋กœ ์ €์žฅ๋˜์–ด [0,1] ํŒ€๊ณผ [2,3] ํŒ€์ด ์ง์ง€์–ด์ง€๊ฒŒ ๋œ๋‹ค.

 

combinations์— ๋Œ€ํ•ด์„œ๋Š” ์•„๋ž˜์˜ ํฌ์ŠคํŒ…์— ์ž์„ธํžˆ ์„ค๋ช…ํ•ด๋‘์—ˆ๋‹ค.

2021.04.01 - [๐Ÿ”์•Œ๊ณ ๋ฆฌ์ฆ˜/๐Ÿ”…๊ฐœ๋…์ •๋ฆฌ] - [ํŒŒ์ด์ฌ/python] ์ˆœ์—ด๊ณผ ์กฐํ•ฉ : itertools ์ด์šฉํ•˜๊ธฐ (permutations, combinations)

 

[ํŒŒ์ด์ฌ/python] ์ˆœ์—ด๊ณผ ์กฐํ•ฉ : itertools ์ด์šฉํ•˜๊ธฐ (permutations, combinations)

itertools๋ฅผ ์ด์šฉํ•˜์—ฌ ์ˆœ์—ด๊ณผ ์กฐํ•ฉ์„ ๊ตฌํ˜„ํ•˜๋ฉด ๋”์šฑ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. 1. ์ˆœ์—ด : permutations(N, r) N์—์„œ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ณ , r๊ฐœ๋ฅผ ๋ฝ‘์•„ ์ˆœ์„œ๋Œ€๋กœ ๋‚˜์—ดํ•œ๋‹ค. ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, ๋ฝ‘

ye333.tistory.com

 

answer = []
for t in range(len(teams) // 2):

    A_stats = 0
    for i in teams[t]:
        for j in teams[t]:
            A_stats += S[i][j]

    B_stats = 0
    for i in teams[-(t + 1)]:
        for j in teams[-(t + 1)]:
            B_stats += S[i][j]

    answer.append(abs(A_stats - B_stats))

print(min(answer))

teams๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‘ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.

๋‘ ํŒ€์”ฉ ์ง์ง“๊ธฐ ๋•Œ๋ฌธ์— range๋Š” teams์˜ ์ ˆ๋ฐ˜์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.

 

ํŒ€์› ๊ฐœ๊ฐœ์ธ์„ ์ง์ง€์€ S[i][j]๋ฅผ ๋Šฅ๋ ฅ์น˜์˜ ํ•ฉ์ธ A_stats์™€ B_stats์— ๋”ํ•œ๋‹ค.

answer์— ๋‘ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜์˜ ์ฐจ์ด์˜ ์ ˆ๋Œ€๊ฐ’์„ ์ €์žฅํ•œ ํ›„ answer์˜ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

728x90

BELATED ARTICLES

more