From ef43d539fd5b564fde000ed96dc6846c78aa0c2d Mon Sep 17 00:00:00 2001 From: Hepheir Date: Tue, 10 Oct 2023 00:18:33 +0900 Subject: [PATCH] =?UTF-8?q?=EB=B0=B1=EC=A4=80=201071=EB=B2=88=20:=20?= =?UTF-8?q?=EC=86=8C=ED=8A=B8=20=ED=92=80=EC=9D=B4=20(=EA=B7=B8=EB=A6=AC?= =?UTF-8?q?=EB=94=94)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1071.py" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 "01\354\243\274\354\260\250/\352\271\200\353\217\231\354\243\274/1071.py" diff --git "a/01\354\243\274\354\260\250/\352\271\200\353\217\231\354\243\274/1071.py" "b/01\354\243\274\354\260\250/\352\271\200\353\217\231\354\243\274/1071.py" new file mode 100644 index 0000000..8f92780 --- /dev/null +++ "b/01\354\243\274\354\260\250/\352\271\200\353\217\231\354\243\274/1071.py" @@ -0,0 +1,32 @@ +MAX_NUM = 1000 + + +def solve(N: int, A: list[int]): + counter = [0] * (MAX_NUM+1) + for a in A: + counter[a] += 1 + build(N, counter) + + +def build(N: int, counter: list[int], stack: list[int] = []): + if len(stack) == N: + print(' '.join(map(str, stack))) + exit() + for n in generate_possible_numbers(counter, stack): + stack.append(n) + counter[n] -= 1 + build(N, counter, stack) + counter[n] += 1 + stack.pop() + + +def generate_possible_numbers(counter: list[int], stack: list[int]): + for i in range(MAX_NUM+1): + if counter[i] > 0 and (not stack or i != stack[-1]+1): + yield i + + +if __name__ == "__main__": + N = int(input()) + A = list(map(int, input().split())) + solve(N, A) \ No newline at end of file