๊ธ€ ์ž‘์„ฑ์ž: ์ด์ง€์›๐ŸŒฉ๏ธ

05:04

N๊ณผ M ์‹œ๋ฆฌ์ฆˆ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์ญˆ์šฑ ํ’€๋ฉด์„œ ์ ์  ์ •๋‹ต ๋น„์œจ์ด ๋†’์•„์ง€๊ธธ๋ž˜ ์™œ ๊ทธ๋Ÿฐ๊ฐ€ ํ–ˆ๋”๋‹ˆ, ์‚ฌ๋žŒ๋“ค์ด ์‹œ๋ฆฌ์ฆˆ ์ˆœ์„œ๋Œ€๋กœ ํ’€๋ฉด์„œ ์œ ํ˜•์„ ํŒŒ์•…ํ•ด์„œ ์ •๋‹ต์œจ์ด ๋Š˜์—ˆ๋‹ค๋Š” ๊ฑธ ์•Œ์•˜๋‹ค. ์™œ๋ƒ๋ฉด ๋‚ด๊ฐ€ ์ง€๊ธˆ ๊ทธ๋Ÿฌ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—…. ์ด์ œ ์•ฝ๊ฐ„ ๋ฐฑํŠธ๋ž™ํ‚น์˜ ๊ฐ์ด ์žกํžŒ๋‹ค.

๋ฌธ์ œ

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


๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

Swift

let input = readLine()!.split(separator: " ").map { Int(String($0))! }
let n = input[0]
let m = input[1]
let nums = readLine()!.split(separator: " ").map { Int(String($0))! }.sorted()
var picked = [String]()
var result = ""
/*
 n: ์ „์ฒด ์›์†Œ์˜ ์ˆ˜
 toPick: ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๋Š” ์›์†Œ์˜ ์ˆ˜
 */
func pick(n: Int, toPick: Int) {
    // ๊ธฐ์ € ์‚ฌ๋ก€: ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๋Š” ์›์†Œ์˜ ์ˆ˜๊ฐ€ ์—†์œผ๋ฉด ์ถœ๋ ฅํ•˜๊ณ  ์ข…๋ฃŒ
    if toPick == 0 {
        result += picked.joined(separator: " ") + "\n"
        return
    }

    // ์ˆ˜๋ฅผ ๊ณ ๋ฅธ๋‹ค. ๊ฐ™์€ ์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ๊ณจ๋ผ๋„ ๋œ๋‹ค.
    for i in 0..<n {
        if let last = picked.last { // ๋น„๋‚ด๋ฆผ์ฐจ์ˆœ ํ™•์ธ
            if Int(last)! > nums[i] {
                continue
            }
        }
        picked.append(String(nums[i]))
        pick(n: n, toPick: toPick - 1)
        picked.removeLast()
    }
}

pick(n: n, toPick: m)

print(result, terminator: "")
๋ฐ˜์‘ํ˜•