ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 1535 ์•ˆ๋…• with Python
    PS 2022. 1. 10. 18:41
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 1535 ์•ˆ๋…•

    ๐Ÿ’ก ์กฐ๊ฑด

    1. ์ฒซ์งธ ์ค„์— ์‚ฌ๋žŒ์˜ ์ˆ˜ N(โ‰ค 20).

    2. ๋‘˜์งธ ์ค„์— ๊ฐ๊ฐ์˜ ์‚ฌ๋žŒ์—๊ฒŒ ์ธ์‚ฌ๋ฅผ ํ•  ๋•Œ, ์žƒ๋Š” ์ฒด๋ ฅ์ด 1๋ฒˆ ์‚ฌ๋žŒ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ž…๋ ฅ.

    3. ์…‹์งธ ์ค„์—๋Š” ๊ฐ๊ฐ์˜ ์‚ฌ๋žŒ์—๊ฒŒ ์ธ์‚ฌ๋ฅผ ํ•  ๋•Œ, ์–ป๋Š” ๊ธฐ์จ์ด 1๋ฒˆ ์‚ฌ๋žŒ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ž…๋ ฅ.

    4. ์ฒด๋ ฅ๊ณผ ๊ธฐ์จ์€ 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜ ๋˜๋Š” 0.

    5. ์„ธ์ค€์ด๊ฐ€ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๊ธฐ์จ์„ ์ถœ๋ ฅ.

    6. ๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜, ๋ฐฐ๋‚ญ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ ํ˜• ๋ฌธ์ œ

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

    from sys import stdin
    
    n = int(stdin.readline())
    stamina_consum = [0] + list(map(int, stdin.readline().split()))
    get_pleasure = [0] + list(map(int, stdin.readline().split()))
    
    dp = [[0] * 101 for _ in range(n + 1)]
    
    for i in range(1, n + 1):
        for j in range(1, 101):
            if stamina_consum[i] <= j:
                dp[i][j] = max(dp[i-1][j], dp[i-1][j - stamina_consum[i]] + get_pleasure[i])
            else:
                dp[i][j] = dp[i-1][j]
    
    print(dp[n][99])

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

    ์˜ˆ์ œ

    3
    1 21 79
    20 30 25

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

    50

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

    1. ๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ˜น์€ ๋ƒ…์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œผ๋กœ ํ’€์ดํ•  ์ˆ˜ ์žˆ๋‹ค.
      ๋‚˜๋Š” ๋ƒ…์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

    2. ์ธ์‚ฌ๋ฅผ ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์Šคํ…Œ๋ฏธ๋„ˆ ์†Œ๋ชจํ•˜๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ stamina_consum ์— [0]์— ์—ฐ๊ฒฐํ•˜์—ฌ ์ €์žฅํ•œ๋‹ค.
      ์ธ์‚ฌ๋ฅผ ํ•  ๋•Œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๊ธฐ์จ ๋ฆฌ์ŠคํŠธ๋ฅผ get_pleasure ์— [0]์— ์—ฐ๊ฒฐํ•˜์—ฌ ์ €์žฅํ•œ๋‹ค.

    3. dp ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค.

      • 1๋ช…๋ถ€ํ„ฐ n๋ช…๊นŒ์ง€ ์ธ์‚ฌ๋ฅผ ํ–ˆ์„ ๋•Œ, ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€์˜ ๊ธฐ์จ์˜ ๊ฐ’์„ ์ €์žฅํ•  ๋ฐฐ์—ด.
      • i๋ฒˆ์งธ ์‚ฌ๋žŒ์—๊ฒŒ ์ธ์‚ฌ๋ฅผ ํ•˜๊ณ , ์Šคํ…Œ๋ฏธ๋„ˆ ์†Œ๋ชจ๋Ÿ‰๋ณด๋‹ค ์ฒด๋ ฅ j๊ฐ€ ํด ๋•Œ ์ธ์‚ฌ๋ฅผ ํ•ด์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๊ธฐ์จ์˜ ์–‘์„ ๊ณ„์‚ฐํ•˜์—ฌ dp์— ์ €์žฅํ•œ๋‹ค.
      • ์ฒด๋ ฅ์ด 1์ด ๊ธฐ์ค€์ด๋ฉฐ, ์ถœ๋ ฅํ•  ๋•Œ dp[n][100]์„ ์ถœ๋ ฅํ•˜๋ฉด ์„ธ์ค€์ด๊ฐ€ ์ธ์‚ฌ๋ฅผ ํ•˜๋‹ค ์ฃฝ์–ด๋ฒ„๋ฆฐ ๊ฒฝ์šฐ์ด๋‹ˆ ์˜ค๋‹ต์ด๋‹ค. dp[n][99]๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
      • ๋งŒ์•ฝ stamina[i] ๋ณด๋‹ค ์ฒด๋ ฅ j ๊ฐ€ ์ ์„ ๊ฒฝ์šฐ, dp[i][j] ๋Š” dp[i-1][j]์˜ ๊ฐ’์„ ๊ฐ€์ ธ๋‹ค ๋„ฃ๋Š”๋‹ค.
      • ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” dp[i-1][j] ์™€ dp[i-1][j - stamina_consum[i]] ์— get_pleasure[i] ๊ฐ’ ์ค‘ ํฐ ๊ฑธ ์ง‘์–ด ๋„ฃ๋Š”๋‹ค.

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

    1. ๋ƒ…์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํ’€์–ด๋„ ํ’€์–ด๋„ ํ—ท๊ฐˆ๋ฆฐ๋‹ค.
    2. ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์•Œ๊ณ ์„œ ํ’€์–ด๋„ ์–ด๋ ต๊ณ , ๊นŒ๋จน์œผ๋ฉด ๋‹ต๋„ ์—†๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
    3. ๋ƒ…์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ์ •๋ฆฌํ•ด์„œ ํ’€์–ด๋ด์•ผํ•  ๊ฒƒ ๊ฐ™๋‹ค.
    4. ๋ฌธ์ œ ํ’€์ด ํ•ด์„ค์„ ์“ฐ๋ฉด์„œ๋„ ํ—ท๊ฐˆ๋ ค์„œ ๋‹ค์‹œ ์ธํ„ฐ๋„ท์„ ๋ณด๊ณ  ์ฐพ์•„์„œ ์ดํ•ดํ•˜๊ณ  ๋ฌธ์ œ๋ฅผ ํ•ด์„ํ•ด์„œ ์ผ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.