์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Python

[BOJ/Step15] 11053 : ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด (Python)

NaNaRin๐Ÿ™ƒ 2021. 2. 26. 20:55

www.acmicpc.net/problem/11053

 

11053๋ฒˆ: ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด

์ˆ˜์—ด A๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ˆ˜์—ด A = {10, 20, 10, 30, 20, 50} ์ธ ๊ฒฝ์šฐ์— ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด์€ A = {10, 20, 10, 30, 20, 50} ์ด

www.acmicpc.net


1. ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜๋“ค์„ ๋ฆฌ์ŠคํŠธ seq์— ์ €์žฅ

2. seq์™€ ๊ฐ™์€ ๊ธธ์ด์˜ t_seq๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋Š” 1, ๋‚˜๋จธ์ง€๋Š” 0์œผ๋กœ ์ฑ„์šด๋‹ค

    => seq[n]๊นŒ์ง€์˜ ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฆฌ์ŠคํŠธ t_seq[n]์— ์ €์žฅํ•  ๊ฒƒ

3. for๋ฌธ์œผ๋กœ t_seq[1] ~ t_seq[n-1] ๊นŒ์ง€ ๊ณ„์‚ฐ

    => ๊ฐ€์žฅ ๊ธด ์ˆ˜์—ด์—๋Š” seq[n]๊ฐ€ ํฌํ•จ๋  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— seq[n]๊ฐ€ ๊ฐ€์žฅ ํฐ ์ˆ˜๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค

    => seq[n] ์ด์ „์˜ seq[n]๋ณด๋‹ค ์ž‘์€ ์ˆซ์ž๋“ค ์ค‘ t_seq[n]๊ฐ€ ๊ฐ€์žฅ ํฐ ์ˆซ์ž์— 1์„ ๋”ํ•ด ์ €์žฅํ•œ๋‹ค

  3-1. ๋ฆฌ์ŠคํŠธ a์— 0์„ ์ €์žฅ

  3-2. for๋ฌธ์œผ๋กœ a์— seq[0] ~ seq[n-1] ์ค‘ seq[n]๋ณด๋‹ค ์ž‘์€ ์ˆซ์ž๋“ค์˜ t_seq[n]์„ ์ €์žฅ

  3-3. a์˜ ์ตœ๋Œ“๊ฐ’ + 1์„ t_seq[n]์— ์ €์žฅ

4. t_seq์˜ ์ตœ๋Œ“๊ฐ’์„ ์ถœ๋ ฅ

 

# 11053.py

n = int(input())
seq = [i for i in map(int, input().split())]
t_seq = [1] + [0 for _ in range(n-1)]

for i in range(1, n):
    a = [0]
    for j in range(i):
        if seq[j] < seq[i]:
            a.append(t_seq[j])
    t_seq[i] = max(a) + 1

print(max(t_seq))

 

์ด์ œ ๋ญ”๊ฐ€ DP๋ฅผ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ํ’€์–ด์•ผ ํ•˜๋Š”์ง€ ์•Œ ๊ฒƒ ๊ฐ™์€ ๋Š๋‚Œ์ด๋‹ค..