Skip to content

Commit 6cdf86d

Browse files
committed
[Gold II] Title: 오름세, Time: 20 ms, Memory: 82360 KB -BaekjoonHub
1 parent ef7fb28 commit 6cdf86d

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [Gold II] 오름세 - 3745
2+
3+
[문제 링크](https://www.acmicpc.net/problem/3745)
4+
5+
### 성능 요약
6+
7+
메모리: 82360 KB, 시간: 20 ms
8+
9+
### 분류
10+
11+
이분 탐색, 가장 긴 증가하는 부분 수열 문제
12+
13+
### 제출 일자
14+
15+
2025년 11월 1일 13:15:44
16+
17+
### 문제 설명
18+
19+
<p>주식투자를 좋아하는 정인이는 주가의 오름세를 살펴보려고 한다.</p>
20+
21+
<p>정인이는 n일 동안 매일 주가를 적어놓았고, 여기서 오름세를 찾아보려고 한다.</p>
22+
23+
<p>n일 동안의 주가를 p<sub>1</sub>, p<sub>2</sub>, ..., p<sub>n</sub>이라고 했을 때, 오름세란 부분수열 p<sub>i<sub>1</sub></sub> < p<sub>i<sub>2</sub></sub> < ... < p<sub>i<sub>k</sub></sub> (i<sub>1</sub> < i<sub>2</sub> < ... i<sub>k</sub>)을 말한다.</p>
24+
25+
<p>n일 동안 주가가 주어졌을 때, 가장 긴 오름세를 찾는 프로그램을 작성하시오.</p>
26+
27+
### 입력
28+
29+
<p>입력은 여러개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스의 첫째 줄에는 주가를 관찰한 날의 수 N (N ≤ 100000)이 주어진다. 둘째 줄에는 관찰한 주가가 첫 날부터 순서대로 주어진다. 주가는 한 개 이상의 공백으로 구분되어 있으며, 그 외의 위치에서도 자유롭게 나올 수 있다. 주가는 100,000보다 작거나 같은 자연수이다.</p>
30+
31+
### 출력
32+
33+
<p>각 테스트 케이스에 대해서 입력으로 주어진 주가의 가장 긴 오름세의 길이를 출력한다.</p>
34+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import Foundation
2+
3+
var answer = ""
4+
var buffer: [UInt8] = Array(FileHandle.standardInput.readDataToEndOfFile()), byteIdx = 0; buffer.append(0)
5+
6+
@inline(__always) func readByte() -> UInt8 {
7+
defer { byteIdx += 1 }
8+
let bp = buffer.withUnsafeBufferPointer { $0[byteIdx] }
9+
if bp == 0 { print(answer); exit(0) } // 여기서 EOF 처리
10+
return bp
11+
}
12+
13+
@inline(__always) func readInt() -> Int {
14+
var number = 0, byte = readByte(), isNegative = false
15+
while byte == 10 || byte == 32 { byte = readByte() }
16+
if byte == 45 { byte = readByte(); isNegative = true }
17+
while 48...57 ~= byte { number = number * 10 + Int(byte - 48); byte = readByte() }
18+
return number * (isNegative ? -1 : 1)
19+
}
20+
21+
func lowerBound(_ x: Int, _ arr: [Int]) -> Int {
22+
var lo = 0, hi = arr.count
23+
24+
while lo < hi {
25+
let mid = (lo + hi) / 2
26+
if arr[mid] < x {
27+
lo = mid + 1
28+
continue
29+
}
30+
hi = mid
31+
}
32+
33+
return lo
34+
}
35+
36+
while true {
37+
let n = readInt()
38+
var arr = [Int]()
39+
40+
for _ in 0..<n {
41+
arr.append(readInt())
42+
}
43+
44+
var dp = [arr[0]]
45+
46+
for i in 1..<n {
47+
let index = lowerBound(arr[i], dp)
48+
if dp.count == index {
49+
dp.append(arr[i])
50+
}
51+
else {
52+
dp[index] = arr[i]
53+
}
54+
}
55+
answer += "\(dp.count)\n"
56+
}

0 commit comments

Comments
 (0)