Skip to content

Commit 1ff06fd

Browse files
committed
b12015 - DP: LIS (n log n)
1 parent 354c257 commit 1ff06fd

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# 12015번: 가장 긴 증가하는 부분 수열 2 - <img src="https://static.solved.ac/tier_small/14.svg" style="height:20px" /> Gold II
2+
3+
<!-- performance -->
4+
5+
<!-- 문제 제출 후 깃허브에 푸시를 했을 때 제출한 코드의 성능이 입력될 공간입니다.-->
6+
7+
<!-- end -->
8+
9+
## 문제
10+
11+
[문제 링크](https://boj.kr/12015)
12+
13+
14+
<p>수열 A가 주어졌을 때, 가장 긴 증가하는&nbsp;부분&nbsp;수열을 구하는 프로그램을 작성하시오.</p>
15+
16+
<p>예를 들어, 수열 A = {10, 20, 10, 30, 20, 50} 인 경우에 가장 긴 증가하는 부분&nbsp;수열은&nbsp;A = {<strong>10</strong>,&nbsp;<strong>20</strong>, 10,&nbsp;<strong>30</strong>, 20,&nbsp;<strong>50</strong>} 이고, 길이는 4이다.</p>
17+
18+
19+
20+
## 입력
21+
22+
23+
<p>첫째 줄에 수열 A의 크기 N (1 ≤ N ≤ 1,000,000)이 주어진다.</p>
24+
25+
<p>둘째 줄에는 수열 A를 이루고 있는 A<sub>i</sub>가 주어진다. (1 ≤ A<sub>i</sub>&nbsp;≤ 1,000,000)</p>
26+
27+
28+
29+
## 출력
30+
31+
32+
<p>첫째 줄에 수열 A의 가장 긴 증가하는 부분 수열의 길이를 출력한다.</p>
33+
34+
35+
36+
## 소스코드
37+
38+
[소스코드 보기](가장%20긴%20증가하는%20부분%20수열%202.py)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# ************************************************************************** #
2+
# #
3+
# ::: ::: ::: #
4+
# Problem Number: 12015 :+: :+: :+: #
5+
# +:+ +:+ +:+ #
6+
# By: ro1864 <boj.kr/u/ro1864> +#+ +#+ +#+ #
7+
# +#+ +#+ +#+ #
8+
# https://boj.kr/12015 #+# #+# #+# #
9+
# Solved: 2024/06/19 10:11:47 by ro1864 ### ### ##.kr #
10+
# #
11+
# ************************************************************************** #
12+
import sys
13+
input = sys.stdin.readline
14+
15+
def binary_search(arr, target, start, end):
16+
while start <= end:
17+
mid = (start + end) // 2
18+
if arr[mid] == target:
19+
return mid
20+
elif arr[mid] > target:
21+
end = mid - 1
22+
else:
23+
start = mid + 1
24+
return start
25+
26+
def lis(p):
27+
dp = []
28+
for i in p:
29+
idx = binary_search(dp, i, 0, len(dp) - 1)
30+
31+
if idx == len(dp):
32+
dp.append(i)
33+
else:
34+
dp[idx] = i
35+
return len(dp)
36+
37+
n = int(input())
38+
a = list(map(int, input().split()))
39+
40+
print(lis(a))

0 commit comments

Comments
 (0)