[๋ฐฑ์ค€ ๐Ÿฅ‡4] 14002๋ฒˆ ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด 4 (Python/ํŒŒ์ด์ฌ)

mingssoยท2023๋…„ 3์›” 19์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
22/35

1๏ธโƒฃ ๋ฌธ์ œ

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



2๏ธโƒฃ ์ฝ”๋“œ

์œ ๋ช…ํ•œ DP ๋ฌธ์ œ์ธ LIS ๋ฌธ์ œ ์‹œ๋ฆฌ์ฆˆ

n = int(input())
arr = list(map(int, input().split()))

dp = [[] for _ in range(n)]
for i in range(n):
    dp[i].append(arr[i])

for i in range(1, n):
    for j in range(i):
        if arr[j] < arr[i]:
            if len(dp[j]) + 1 > len(dp[i]):
                dp[i] = dp[j] + [arr[i]]

answer = []
for i in range(n):
    if len(dp[i]) > len(answer):
        answer = dp[i]

answer.sort()   # ์ฒ˜์Œ์— dp์— arr์˜ ๊ฐ ์›์†Œ๋ฅผ ๋„ฃ์–ด ์ดˆ๊ธฐํ™” ์‹œ์ผœ์คฌ๊ธฐ ๋•Œ๋ฌธ์— ์ •๋ ฌ์„ ๋‹ค์‹œ ํ•ด์ค˜์•ผ ํ•จ
print(len(answer))
print(' '.join(map(str, answer)))

์ด ๋ฌธ์ œ ํ’€์ด ๊ฒ€์ƒ‰ํ•ด๋ณด๋ฉด ๋Œ€๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ๊ธธ์ด ๋จผ์ € ๊ตฌํ•˜๊ณ  ์—ญ์ถ”์ ํ•ด์„œ ์ˆ˜์—ด ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์—ˆ๋˜๋ฐ ๋‚˜๋Š” ํ•œ๊บผ๋ฒˆ์— ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์—ˆ๋‹ค

profile
๐Ÿฅ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ’ฐ

0๊ฐœ์˜ ๋Œ“๊ธ€