Skip to content

Commit c6abf63

Browse files
committed
[Gold II] Title: 가장 긴 증가하는 부분 수열 3, Time: 612 ms, Memory: 144720 KB -BaekjoonHub
1 parent 4b2172e commit c6abf63

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [Gold II] 가장 긴 증가하는 부분 수열 3 - 12738
2+
3+
[문제 링크](https://www.acmicpc.net/problem/12738)
4+
5+
### 성능 요약
6+
7+
메모리: 144720 KB, 시간: 612 ms
8+
9+
### 분류
10+
11+
이분 탐색, 가장 긴 증가하는 부분 수열 문제
12+
13+
### 제출 일자
14+
15+
2025년 11월 1일 13:54:21
16+
17+
### 문제 설명
18+
19+
<p>수열 A가 주어졌을 때, 가장 긴 증가하는 부분 수열을 구하는 프로그램을 작성하시오.</p>
20+
21+
<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>
22+
23+
### 입력
24+
25+
<p>첫째 줄에 수열 A의 크기 N (1 ≤ N ≤ 1,000,000)이 주어진다.</p>
26+
27+
<p>둘째 줄에는 수열 A를 이루고 있는 A<sub>i</sub>가 주어진다. (-1,000,000,000 ≤ A<sub>i</sub> ≤ 1,000,000,000)</p>
28+
29+
### 출력
30+
31+
<p>첫째 줄에 수열 A의 가장 긴 증가하는 부분 수열의 길이를 출력한다.</p>
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
let n = Int(readLine()!)!
2+
let arr = readLine()!.split { $0 == " " }.map { Int(String($0))! }
3+
var lis = [arr[0]]
4+
5+
// 즉 lis를 만족하기 위해 몇개의 수를 스킵해야 하냐는 문제. 전선을 잘라내야 하냐는 문제.
6+
7+
for x in arr.dropFirst() {
8+
let index = lowerBound(x, lis)
9+
if lis.count == index {
10+
lis.append(x)
11+
continue
12+
}
13+
lis[index] = x
14+
}
15+
print(lis.count)
16+
17+
func lowerBound(_ x: Int, _ arr: [Int]) -> Int {
18+
var lo = 0, hi = arr.count
19+
20+
while lo < hi {
21+
let mid = (lo + hi) / 2
22+
if arr[mid] < x {
23+
lo = mid + 1
24+
continue
25+
}
26+
hi = mid
27+
}
28+
29+
return lo
30+
}

0 commit comments

Comments
 (0)