ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

10819๋ฒˆ: ์ฐจ์ด๋ฅผ ์ตœ๋Œ€๋กœ

์ฒซ์งธ ์ค„์— N (3 ≤ N ≤ 8)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ๋ฐฐ์—ด A์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋ฐฐ์—ด์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜๋Š” -100๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์•ž์˜ 10974 ๋ชจ๋“  ์ˆœ์—ด์—์„œ ํŠน์ • ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ๋กœ ๋ณ€๊ฒฝ๋œ ๋ฌธ์ œ์ด๋‹ค. ์ด ์—ญ์‹œ DFS๋ฅผ ํ†ตํ•ด ์ˆœ์—ด์„ ๊ตฌํ•˜๋Š” ๋ฒ•์„ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค. ์ž…๋ ฅ๋˜๋Š” ๊ฐ’๋“ค์€ ์ •๋ ฌ๋œ ์ƒํƒœ๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ, DFS๋ฅผ ํ†ตํ•ด ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์ „์— ์ •๋ ฌ์ด ํ•„์š”ํ•˜๋‹ค.

 

์ฝ”๋“œ

DFS๋ฅผ ์‚ฌ์šฉํ•œ ๋ฌธ์ œ ํ’€์ด

from sys import stdin


def dfs(depth):
    global answer

    if depth == n:
        answer.append([nums[i] for i in check])
    else:
        for i in range(n):
            if i in check:
                continue
            check[depth] = i
            dfs(depth + 1)
            check[depth] = -1

if __name__ == '__main__':
    answer = []
    n = int(stdin.readline())
    nums = sorted(list(map(int, stdin.readline().split())))
    check = [-1] * n
    dfs(0)
    min_case = 0
    for case in answer:
        sum_case = 0
        for i in range(n - 1):
            sum_case += abs(case[i] - case[i + 1])
        min_case = max(min_case, sum_case)
    print(min_case)

 

itertools๋ฅผ ํ™œ์šฉํ•œ ๋ฌธ์ œ ํ’€์ด

from itertools import permutations
N = int(input())
cases = permutations(sorted(list(map(int, input().split()))))
answer = 0

for case in cases:
    sum_case = 0
    for i in range(N - 1):
        sum_case += abs(case[i]-case[i + 1])
    answer = max(answer, sum_case)
print(answer)
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€