ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

16936๋ฒˆ: ๋‚˜3๊ณฑ2

๋‚˜3๊ณฑ2 ๊ฒŒ์ž„์€ ์ •์ˆ˜ ํ•˜๋‚˜๋ฅผ ์ด์šฉํ•œ๋‹ค. ๊ฐ€์žฅ ๋จผ์ €, ์ •์ˆ˜ x๋กœ ์‹œ์ž‘ํ•˜๊ณ , ์—ฐ์‚ฐ์„ N-1๋ฒˆ ์ ์šฉํ•œ๋‹ค. ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์—ฐ์‚ฐ์€ ๋‘ ๊ฐ€์ง€ ์žˆ๊ณ , ์•„๋ž˜์™€ ๊ฐ™๋‹ค. ๋‚˜3: x๋ฅผ 3์œผ๋กœ ๋‚˜๋ˆˆ๋‹ค. x๋Š” 3์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ ธ์•ผ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์ •์ˆ˜ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฐ์‚ฐ์„ N-1๋ฒˆ ์ ์šฉํ•œ๋‹ค. ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์—ฐ์‚ฐ์€ ์ •์ˆ˜๊ฐ€ 3์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ๊ฒฝ์šฐ 3์œผ๋กœ ๋‚˜๋ˆ„๊ฑฐ๋‚˜, 2๋ฅผ ๊ณฑํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ๋ฌธ์ œ์— ์ œ์‹œ๋œ ์กฐ๊ฑด์— ๋”ฐ๋ผ DFS๋ฅผ ํ†ตํ•˜์—ฌ ๊ตฌํ•˜๋ฉด ์‰ฝ๊ฒŒ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋‹ค. DFS๋กœ ํ‘ธ๋Š” ๊ฒƒ์€ ๋‹ค๋ฅธ ๋ฌธ์ œ์—์„œ๋„ ๋งŽ์ด ๋‹ค๋ฃจ์—ˆ๊ณ  `์„œ๋กœ์†Œ`์˜ ์„ฑ์งˆ์„ ์ด์šฉํ•˜์—ฌ 3์œผ๋กœ ๊ฐ€์žฅ ๋งŽ์ด ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ˆ˜๋ถ€ํ„ฐ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

 

๋งŒ์•ฝ, DFS๋กœ ํ’€๊ณ ์ž ํ•œ๋‹ค๋ฉด DFS์˜ deapth๊ฐ€ N - 1์ด ๋ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๊ณ  3์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ๊ฒฝ์šฐ์™€, 2๋ฅผ ๊ณฑํ•˜๋Š” ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ํƒ์ƒ‰ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin


if __name__ == '__main__':
    n = int(stdin.readline())
    b = list(map(int, stdin.readline().split()))
    b = {num: 0 for num in b}

    for num in b.keys():
        cur_num = num

        while True:
            if num % 3 == 0:
                b[cur_num] += 1
                num //= 3
            else:
                break

    sort_dict = sorted(b.items(), key=lambda x: (-x[1], x[0]))
    print(*[key for key, _ in sort_dict])

 

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€