ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 15486 ํ‡ด์‚ฌ 2 with Python
    PS 2021. 8. 29. 23:46
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 15486 ํ‡ด์‚ฌ 2

    ๐Ÿ’ก ์กฐ๊ฑด ๋ฐ ํ’€์ด

    1. ํ‡ด์‚ฌ๊ฐ€ ๋‚จ์€ ์ผ ์ˆ˜ N.
    2. 1 <= N <= 1500000
    3. T, P ์˜ ๊ธธ์ด๋Š” N๊ณผ ๊ฐ™์œผ๋ฉฐ, 1 <= Ti <= 50, 1 <= Pi <= 1000
    4. N + 1 ์— ํ•ด๋‹นํ•˜๋Š” ๋‚ ์งœ๋ถ€ํ„ฐ๋Š” ์ƒ๋‹ด์„ ํ•  ์ˆ˜ ์—†๋‹ค.
    5. DP ์œ ํ˜•์˜ ๋ฌธ์ œ
    6. ์ƒ๋‹ด์„ ํ†ตํ•ด ์ทจํ•œ ์ด์ต ์ค‘, ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ.

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

    from sys import stdin
    
    n = int(stdin.readline())
    t, p = [], []
    dp = [0 for _ in range(n + 1)]
    
    for _ in range(n):
        ti, pi = map(int, stdin.readline().split())
        t.append(ti)
        p.append(pi)
    
    k = 0
    for i in range(n):
        k = max(k, dp[i])
        if i + t[i] > n:
            continue
        dp[i + t[i]] = max(k + p[i], dp[i + t[i]])
    
    print(max(dp))

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

    ์˜ˆ์ œ

    10
    5 50
    4 40
    3 30
    2 20
    1 10
    1 10
    2 20
    3 30
    4 40
    5 50

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

    90

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

    1. ํŽธํ•œ ๊ณ„์‚ฐ์„ ์œ„ํ•ด dp๋ฅผ n + 1์˜ ํฌ๊ธฐ๋งŒํผ ์ƒ์„ฑํ–ˆ๋‹ค.
    2. ์ƒ๋‹ด์„ ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์‹œ๊ฐ„์„ ๋‹ด์„ ๋ฆฌ์ŠคํŠธ T๋ฅผ ์ƒ์„ฑ
    3. ์ƒ๋‹ด์„ ํ•˜๋ฉด ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ด์ต์„ ๋‹ด์„ ๋ฆฌ์ŠคํŠธ P๋ฅผ ์ƒ์„ฑ
    4. 1์ผ์งธ๋ถ€ํ„ฐ ์ƒ๋‹ด์„ ์ง„ํ–‰ํ•˜๋ฉด ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ด์ต์„ DP ๋ฆฌ์ŠคํŠธ์— ์ตœ๋Œ“๊ฐ’์œผ๋กœ ๊ฐฑ์‹ 
    5. ์ƒ๋‹ด์„ ์‹œ์ž‘ํ•œ ๋‚ ์˜ ์ด์ต์˜ ๋ฐ์ดํ„ฐ๊ฐ€ DP ๋ฆฌ์ŠคํŠธ์˜ ์ƒ๋‹ด์ด ๋๋‚˜๋Š” ๋‚  ์œ„์น˜์— ๋“ค์–ด๊ฐ
    6. i๋ฒˆ์งธ ๋‚ ์— ์ƒ๋‹ด์„ ์‹œ์ž‘ํ–ˆ์œผ๋ฉด, i + t[i] ๊ฐ€ n์„ ๋„˜์ง€ ๋ง์•„์•ผํ•œ๋‹ค.
      ์ฆ‰, 7(N)์ผ ๋‚จ์€ ํ‡ด์‚ฌ ์˜ˆ์ •์ž๊ฐ€ ํ‡ด์‚ฌ ํ•˜๋ฃจ ์ „ ๋‚ (i = 7)์—
      T[i] 2์ผ ๊ฑธ๋ฆด ์ƒ๋‹ด์„ ์‹œ์ž‘ํ•˜๋ฉด, ๋๋‚ด์ง€ ๋ชปํ•˜๊ธฐ์—(N < Ti + i) ์ด์ต์„ ์ทจํ•  ์ˆ˜ ์—†๋‹ค.
    7. k ๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด, dp[i]์™€ k ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ง‘์–ด๋„ฃ๋Š”๋‹ค.
      ์ด ๊ฐ’๊ณผ dp[i + t[i]] ์ด ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ํฐ ๊ฐ’์„ dp[i + t[i]]์— ๋„ฃ๋Š”๋‹ค.

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

    • ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฌธ์ œ์— ๋งค์šฐ ์•ฝํ•˜๋‹ค. ์—ฌ๋Ÿฌ ๋ฌธ์ œ๋ฅผ ๊พธ์ค€ํžˆ ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค.
    • ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ถ€๋ถ„์—์„œ๋Š” ๊ตฌํ˜„ ๋ถ€๋ถ„์—์„œ ๊ต‰์žฅํžˆ ํ—ค๋งค๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
      ๊ฐ ๋ณ€์ˆ˜์˜ ์‚ฌ์šฉ์ฒ˜, ๊ฐฑ์‹  ๋“ฑ์„ ๋” ๊ผผ๊ณฐํžˆ ์ฒดํฌํ•ด์„œ ์‚ฌ๊ณ ํ•˜๋Š” ๋Šฅ๋ ฅ์„ ๋” ํ‚ค์›Œ์•ผ๊ฒ ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.