[BaekJoon] ๋ฐฑ์ค€ 15657๋ฒˆ : N๊ณผ M (8)

Updated:

15657๋ฒˆ : N๊ณผ M (8)


[BaekJoon] ๋ฐฑ์ค€ 15654๋ฒˆ : N๊ณผ M (5) ์™€ ๊ฐ™์€ ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

15654๋ฒˆ ๋ฌธ์ œ์™€ ๊ฐ™์ง€๋งŒ ์ˆœ์„œ๊ฐ€ ์—†๊ณ  ๊ฐ™์€ ์ˆซ์ž๋„ ๋ฝ‘์„ ์ˆ˜์žˆ๊ฒŒ ํ’€๋ฉด ๋œ๋‹ค.


import sys


def DFS():
	global n, m, data, stack

	if len(stack) == m:
		print(*stack)
		return

	for i in range(n):
		if stack and stack[-1] > data[i]:
			continue

		stack.append(data[i])
		DFS()
		stack.pop()


n, m = map(int, sys.stdin.readline().rsplit())
data = sorted(list(map(int, sys.stdin.readline().rsplit())))
stack = []
DFS()

import sys
from itertools import combinations_with_replacement


n, m = map(int, sys.stdin.readline().rsplit())
data = sorted(list(map(int, sys.stdin.readline().rsplit())))
res = combinations_with_replacement(data, m)

for val in res:
	print(*val)

๊ธฐ๋ณธ์ ์œผ๋กœ ์กฐํ•ฉ์ด์ง€๋งŒ ์ž๊ธฐ ์ž์‹ ๋„ ์ค‘๋ณต์„ ๋ฝ‘์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— combination_with_replacement ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.



Categories:

Updated:

Leave a comment