Skip to content

Commit 4255375

Browse files
committed
[Gold V] Title: 용액, Time: 36 ms, Memory: 82548 KB -BaekjoonHub
1 parent b622c47 commit 4255375

2 files changed

Lines changed: 47 additions & 33 deletions

File tree

백준/Gold/2467. 용액/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 78392 KB, 시간: 64 ms
7+
메모리: 82548 KB, 시간: 36 ms
88

99
### 분류
1010

1111
이분 탐색, 두 포인터
1212

1313
### 제출 일자
1414

15-
2025년 5월 9일 00:16:18
15+
2025년 6월 6일 20:59:34
1616

1717
### 문제 설명
1818

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,49 @@
1-
let n = Int(readLine()!)!
2-
let valueList = readLine()!.split { $0 == " " }.map { Int(String($0))! }
3-
var minSum = Int.max,
4-
v1 = Int.max,
5-
v2 = Int.max
6-
var start = 0,
7-
end = n-1
8-
while true {
9-
if start == end {
10-
break
1+
import Foundation
2+
3+
class FileIO {
4+
@inline(__always) private var buffer: [UInt8] = Array(FileHandle.standardInput.readDataToEndOfFile()) + [0], byteIdx = 0
5+
6+
@inline(__always) private func readByte() -> UInt8 {
7+
defer { byteIdx += 1 }
8+
return buffer.withUnsafeBufferPointer { $0[byteIdx] }
119
}
12-
// 특성값의 합이 전보다 작으면, 특성값을 갱신한다.
13-
let curSum = valueList[start] + valueList[end]
14-
if abs(0-curSum) < abs(0-minSum) {
15-
v1 = valueList[start]
16-
v2 = valueList[end]
17-
minSum = valueList[start] + valueList[end]
18-
}
19-
// 특성값의 합이 0이므로, 종료한다.
20-
if minSum == 0 { break }
21-
// 특성값의 합이 0보다 크므로, 큰 수를 작게 한다.
22-
if curSum > 0 {
23-
end -= 1
24-
continue
25-
}
26-
// 특성값의 합이 0보다 작으므로, 작은수를 크게 한다.
27-
if curSum < 0 {
28-
start += 1
29-
continue
10+
11+
@inline(__always) func readInt() -> Int {
12+
var number = 0, byte = readByte(), isNegative = false
13+
while byte == 10 || byte == 32 { byte = readByte() }
14+
if byte == 45 { byte = readByte(); isNegative = true }
15+
while 48...57 ~= byte { number = number * 10 + Int(byte - 48); byte = readByte() }
16+
return number * (isNegative ? -1 : 1)
3017
}
3118
}
32-
@inline(__always) func abs(_ n: Int) -> Int {
33-
return n > 0 ? n : -n
19+
20+
let io = FileIO()
21+
22+
let n = io.readInt()
23+
let values = (0..<n).map { _ in io.readInt() }.sorted { $0 < $1 }
24+
25+
var min = Int.max
26+
var answers = [Int]()
27+
28+
for i in 0..<n-1 {
29+
binSearch(i+1)
30+
func binSearch(_ s: Int){
31+
var s = s
32+
var e = values.count-1
33+
34+
while s <= e {
35+
let mid = (s+e)/2
36+
let sum = abs(values[i] + values[mid])
37+
if min > sum {
38+
min = sum
39+
answers = [values[i], values[mid]]
40+
}
41+
if values[mid] >= -values[i] {
42+
e = mid-1
43+
} else {
44+
s = mid+1
45+
}
46+
}
47+
}
3448
}
35-
print(v1, v2)
49+
print( answers.sorted { $0 < $1}.map { "\($0) " }.joined())

0 commit comments

Comments
 (0)