๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
4๏ธโƒฃ Python/Problem Solving

[Python Algorithm] ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ์ˆ˜์—ด BOJ #11053

by seolhee2750 2022. 1. 30.
๋ฌธ์ œ

https://www.acmicpc.net/problem/11053

 

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

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

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
n = int(input())
permu = list(map(int, input().split()))
count = [1] # ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž์˜ ๊ธธ์ด๋Š” ์–ด์ฐจํ”ผ 1์ด๋ฏ€๋กœ ๋ฏธ๋ฆฌ ์ถ”๊ฐ€
counting = 0

def find(now):
    global counting
    for i in reversed(range(len(count))):
        if counting < count[i] and permu[i] < now:
            counting = count[i]

for i in range(1, n):
    find(permu[i])
    count.append(counting+1)
    counting = 0

print(max(count))

๐Ÿ‘‰ DP ๋ฌธ์ œ๋กœ, ๋‚˜๋ณด๋‹ค ์ž‘์€ ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ์ˆ˜์—ด์— ๋Œ€ํ•œ ์นด์šดํŠธ๋ฅผ ๋ˆ„์ ํ•˜๋Š” ๊ฒƒ์ด ํฌ์ธํŠธ !

  • ์ฃผ์–ด์ง„ permu ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ๋Œ๋ฉด์„œ find ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด์ฃผ์—ˆ๋‹ค.
    find ํ•จ์ˆ˜๋Š” ํ˜„์žฌ ์ˆซ์ž๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š”๋ฐ, ํ˜„์žฌ ์ˆซ์ž ์ด์ „์˜ ์ˆซ์ž๋“ค์„ ๊ฒ€์‚ฌํ•˜๋ฉด์„œ,
    ํ˜„์žฌ ์ˆซ์ž๋ณด๋‹ค ์ž‘์€ ์ˆ˜ ์ค‘์— count์— ์ €์žฅ๋œ ๊ฐ’์ด ๊ฐ€์žฅ ํฐ ๊ฐ’์„ counting์— ์ €์žฅํ–ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  find์—์„œ ๊ตฌํ•œ counting์— 1์„ ๋”ํ•œ ๊ฐ’์„ count ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ–ˆ๊ณ ,
    ๋‹ค์Œ ๋ฐ˜๋ณต์ด ์‹œํ–‰๋˜๊ธฐ ์ „ counting ๋ณ€์ˆ˜๋Š” ๋‹ค์‹œ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ–ˆ๋‹ค.
  • ์ด๋Ÿฌํ•œ ์‹œํ–‰์„ ๋ฐ˜๋ณตํ•˜์—ฌ count์— ์ €์žฅ๋œ ๊ฐ’ ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ถœ๋ ฅํ–ˆ๋‹ค.

โž• ์ด ๋ฌธ์ œ๋Š” ์•„์ฃผ ์ „ํ˜•์ ์ธ LIS ๋ฌธ์ œ์ด๋ฏ€๋กœ, ๊ฐ™์ด ๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„ ์ถ”๊ฐ€ํ•œ๋‹ค.

https://seolhee2750.tistory.com/109?category=872093 

 

[Swift Algorithm Note] ์ตœ์žฅ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด (feat. DP)

์˜ค๋Š˜์€ ์ตœ์žฅ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด (LIS)์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด๋ คํ•œ๋‹ค. LIS๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€์ง€๋งŒ, ๊ทธ ์ค‘์—์„œ๋„ ๊ฐ€์žฅ ๊ฐ„ํŽธํ•œ DP๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•œ๋‹ค. (DP์— ๋Œ€ํ•ด ๊ถ๊ธˆํ•˜์‹  ๋ถ„๋“ค์€ ์š”๊ธฐ๐Ÿ‘‡๋ฅผ ์ฐธ๊ณ 

seolhee2750.tistory.com

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์‰ฌ์šด ๋ฌธ์ œ์ง€๋งŒ, LIS ์œ ํ˜•์„ ๋ชจ๋ฅธ๋‹ค๋ฉด ์ƒ๊ฐํ•˜๊ธฐ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๊ฐ™๋‹ค.
    ๋‚˜๋„ DP ์ž์ฒด๋ฅผ ์˜ค๋žœ๋งŒ์— ํ’€์—ˆ๋”๋‹ˆ ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ ธ๋‹ค.

๋Œ“๊ธ€