ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 1094 ๋ง‰๋Œ€๊ธฐ with Python
    PS 2022. 2. 1. 02:14
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 1094 ๋ง‰๋Œ€๊ธฐ

    ๐Ÿ’ก ์กฐ๊ฑด

    1. 64cm์ธ ๋ง‰๋Œ€๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
      ์ง€๋ฏผ์ด๋Š” ์›๋ž˜ ๊ฐ€์ง€๊ณ  ์žˆ๋˜ ๋ง‰๋Œ€๋ฅผ ๋” ์ž‘์€ ๋ง‰๋Œ€๋กœ ์ž๋ฅธ๋‹ค์Œ์—, ํ’€๋กœ ๋ถ™์—ฌ์„œ ๊ธธ์ด๊ฐ€ Xcm์ธ ๋ง‰๋Œ€๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค.

    2. ์•„๋ž˜๋Š” ๋ง‰๋Œ€๋ฅผ ์ž๋ฅด๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.
      1) ์ง€๋ฏผ์ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ง‰๋Œ€์˜ ๊ธธ์ด๋ฅผ ๋ชจ๋‘ ๋”ํ•œ๋‹ค.
      ์ฒ˜์Œ์—๋Š” 64cm ๋ง‰๋Œ€ ํ•˜๋‚˜๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ด๋•Œ, ํ•ฉ์ด X๋ณด๋‹ค ํฌ๋‹ค๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์€ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
      1-1) ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ง‰๋Œ€ ์ค‘ ๊ธธ์ด๊ฐ€ ๊ฐ€์žฅ ์งง์€ ๊ฒƒ์„ ์ ˆ๋ฐ˜์œผ๋กœ ์ž๋ฅธ๋‹ค.
      1-2) ๋งŒ์•ฝ, ์œ„์—์„œ ์ž๋ฅธ ๋ง‰๋Œ€์˜ ์ ˆ๋ฐ˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ๋ฒ„๋ฆฌ๊ณ  ๋‚จ์•„์žˆ๋Š” ๋ง‰๋Œ€์˜ ๊ธธ์ด์˜ ํ•ฉ์ด X๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๋ฉด,

          ์œ„์—์„œ ์ž๋ฅธ ๋ง‰๋Œ€์˜ ์ ˆ๋ฐ˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ๋ฒ„๋ฆฐ๋‹ค.

      2) ์ด์ œ, ๋‚จ์•„์žˆ๋Š” ๋ชจ๋“  ๋ง‰๋Œ€๋ฅผ ํ’€๋กœ ๋ถ™์—ฌ์„œ Xcm๋ฅผ ๋งŒ๋“ ๋‹ค.

    3. X๋Š” 64๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜

    4. DFS, ์ˆ˜ํ•™, ๋น„ํŠธ๋งˆ์Šคํ‚น ์œ ํ˜•์˜ ๋ฌธ์ œ

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from sys import stdin, setrecursionlimit
    import heapq
    setrecursionlimit(10 ** 6)
    
    x = int(stdin.readline())
    res = 0
    sticks = [64]
    
    
    def solve(sticks):
        global res
        val = sum(sticks)
    
        if val > x:
            temp = heapq.heappop(sticks)
            heapq.heappush(sticks, temp // 2)
            heapq.heappush(sticks, temp // 2)
    
            if sum(sticks) - (temp // 2) >= x:
                heapq.heappop(sticks)
                solve(sticks)
            elif sum(sticks) >= x:
                solve(sticks)
    
    
    solve(sticks)
    print(len(sticks))

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    23

    ์‹คํ–‰๊ฒฐ๊ณผ

    4

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. DFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์„œ ํ’€์ดํ–ˆ๋‹ค.

    2. ๊ฒฐ๊ณผ๊ฐ’์„ ์ €์žฅํ•  res ๋ณ€์ˆ˜์™€ ๋ถ„๋ฆฌ๋˜์ง€ ์•Š์€ ๋ง‰๋Œ€๊ธฐ 64๊ฐ€ ๋“ค์–ด๊ฐ€์žˆ๋Š” sticks ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

    3. solve() ํ•จ์ˆ˜์˜ ๋กœ์ง์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

      • sticks ๋ฆฌ์ŠคํŠธ์˜ ํ•ฉ์ด ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’ x ๋ณด๋‹ค ํด ๊ฒฝ์šฐ,
        sticks ์—์„œ heapq.heappop() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ๋นผ๋‚ธ๋‹ค.
        ๋นผ๋‚ธ ๊ฐ’์„ 2๋กœ ๋‚˜๋ˆˆ ๋ชซ์„ ๋‹ค์‹œ heapq.heappush()๋กœ ๋‹ค์‹œ ๋„ฃ์–ด์ค€๋‹ค.

      • ๋งŒ์•ฝ, sticks ๋ฆฌ์ŠคํŠธ์˜ ํ•ฉ์—์„œ heapq.heappop() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ๋บ€ ๊ฐ’์ด x๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๋ฉด
        heapq.heappop() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ๋นผ๋‚ธ๋‹ค.
        solve() ํ•จ์ˆ˜๋ฅผ ์žฌ๊ท€ํ˜ธ์ถœํ•œ๋‹ค.

      • ์œ„์˜ ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜์ง€ ์•Š๊ณ , sticks์˜ ๋ฆฌ์ŠคํŠธ์˜ ํ•ฉ์ด x๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๋ฉด ๋‹ค์‹œ solve() ํ•จ์ˆ˜๋ฅผ ์žฌ๊ท€ํ˜ธ์ถœํ•œ๋‹ค.

    4. sticks ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

    ๐Ÿ’พ ๋Š๋‚€์ 

    1. heapq๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์žฌ๊ท€ํ˜ธ์ถœ์„ ํ†ตํ•ด ๋‹ต์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.
    2. ๋ง‰๋Œ€๊ธฐ๋ฅผ ์ž๋ฅด๋Š” ๋ฐฉ๋ฒ•์„ ์ถฉ์‹คํžˆ ๊ตฌํ˜„ํ•˜์—ฌ ์žฌ๊ท€ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์–ด๋ ต์ง€ ์•Š๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.