[Gold IV] Title: 가장 긴 증가하는 부분 수열 4, Time: 108 ms, Memory: 34476 KB -BaekjoonHub

This commit is contained in:
SSUM
2025-03-31 13:27:40 +09:00
parent be1fe63154
commit ea049b46a7
2 changed files with 64 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
# [Gold IV] 가장 긴 증가하는 부분 수열 4 - 14002
[문제 링크](https://www.acmicpc.net/problem/14002)
### 성능 요약
메모리: 34476 KB, 시간: 108 ms
### 분류
다이나믹 프로그래밍
### 제출 일자
2025년 3월 31일 13:27:19
### 문제 설명
<p>수열 A가 주어졌을 때, 가장 긴 증가하는 부분 수열을 구하는 프로그램을 작성하시오.</p>
<p>예를 들어, 수열 A = {10, 20, 10, 30, 20, 50} 인 경우에 가장 긴 증가하는 부분 수열은 A = {<strong>10</strong>, <strong>20</strong>, 10, <strong>30</strong>, 20, <strong>50</strong>} 이고, 길이는 4이다.</p>
### 입력
<p>첫째 줄에 수열 A의 크기 N (1 ≤ N ≤ 1,000)이 주어진다.</p>
<p>둘째 줄에는 수열 A를 이루고 있는 A<sub>i</sub>가 주어진다. (1 ≤ A<sub>i</sub> ≤ 1,000)</p>
### 출력
<p>첫째 줄에 수열 A의 가장 긴 증가하는 부분 수열의 길이를 출력한다.</p>
<p>둘째 줄에는 가장 긴 증가하는 부분 수열을 출력한다. 그러한 수열이 여러가지인 경우 아무거나 출력한다.</p>

View File

@@ -0,0 +1,30 @@
import sys
from bisect import bisect_left
input = sys.stdin.readline
n = int(input())
lst = list(map(int, input().split()))
dp_table = [1 for i in range(n+1)]
prev = [-1 for i in range(n+1)]
for i in range(n):
for j in range(i):
if lst[i] > lst[j] and dp_table[i] < dp_table[j] + 1:
dp_table[i] = dp_table[j] + 1
prev[i] = j
k = max(dp_table)
idx = dp_table.index(k)
result = []
while idx!=-1:
result.append(lst[idx])
idx = prev[idx]
result.reverse()
print(k)
print(*result)