N과M (10)

김준오·2021년 8월 29일
0

알고리즘

목록 보기
49/91
post-thumbnail

문제

boj 15655 N과M (10)

풀이

import sys
input = sys.stdin.readline
from itertools import combinations
from itertools import permutations
from itertools import product
from itertools import combinations_with_replacement

n,m = map(int,input().split())
menu = list(map(int,input().split()))
menu.sort()
record = dict()
for i in combinations(menu,m):
  if i not in record:
    record[i] = 1
    print(' '.join(map(str,i)))

재귀풀이

n, m = map(int,input().split())

arr = list(map(int,input().split()))
# arr.sort()
used = [0] * len(arr)

answer = []

def comb(lst):
    if len(lst) == m:
        answer.append(tuple(lst))
        return


    for i in range(len(arr)):
        if used[i] == 1:
            continue
        
        if lst and arr[i] < max(lst):
            continue
        
        used[i] = 1
        comb(lst + [arr[i]])
        used[i] = 0

comb([])
answer = sorted(list(set(answer)))
for tp in answer:
    for i in tp:
        print(i, end=' ')
    print()
profile
jooooon

0개의 댓글

관련 채용 정보