Gemstone's Devlog

[백준] 10819번: 차이를 최대로 본문

Data Structure & Algorithms

[백준] 10819번: 차이를 최대로

Gemstone 2022. 4. 13. 01:53

 

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

 

10819번: 차이를 최대로

첫째 줄에 N (3 ≤ N ≤ 8)이 주어진다. 둘째 줄에는 배열 A에 들어있는 정수가 주어진다. 배열에 들어있는 정수는 -100보다 크거나 같고, 100보다 작거나 같다.

www.acmicpc.net

이 문제는 브루트포스 알고리즘으로 접근해야 하는 문제이다.

 

파이썬 모듈 안에 내장되어있는 조합 라이브러리를 사용했다.

 

파이썬 풀이

from itertools import permutations
import sys

# 주어진 값 입력
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))

# permutation 저장(per: reference of permutation tuples)
per = permutations(a)
ans = 0

# 순열마다 차이를 더해(s), ans 보다 크면 ans를 update
for i in per:
    s = 0
    for j in range(len(i) - 1):
        s += abs(i[j] - i[j + 1])
    if s > ans:
        ans = s

print(ans)