Skip to content

Commit b0656a0

Browse files
committed
[Gold V] Title: 센서, Time: 12 ms, Memory: 70100 KB -BaekjoonHub
1 parent 6665e65 commit b0656a0

2 files changed

Lines changed: 11 additions & 28 deletions

File tree

백준/Gold/2212. 센서/README.md

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

55
### 성능 요약
66

7-
메모리: 79856 KB, 시간: 12 ms
7+
메모리: 70100 KB, 시간: 12 ms
88

99
### 분류
1010

1111
그리디 알고리즘, 정렬
1212

1313
### 제출 일자
1414

15-
2025년 6월 17일 20:57:37
15+
2025년 8월 21일 13:24:46
1616

1717
### 문제 설명
1818

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
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] }
9-
}
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)
17-
}
1+
let n = Int(readLine()!)!
2+
let k = Int(readLine()!)!
3+
4+
let sensors = readLine()!.split { $0 == " " }.map { Int(String($0))! }.sorted { $0 < $1 }
5+
var distances = [Int]()
6+
for i in 1..<n {
7+
let distance = sensors[i] - sensors[i-1]
8+
distances.append(distance)
189
}
1910

20-
let io = FileIO()
21-
22-
let n = io.readInt()
23-
let m = io.readInt()
24-
25-
let sensors = (0..<n).map { _ in io.readInt() }.sorted { $0 < $1 }
26-
let distances = (0..<sensors.count-1).map { i in sensors[i+1] - sensors[i] }.sorted { $0 < $1 }
27-
let answer = distances.prefix(max(distances.count - (m-1), 0)).reduce(0, +)
28-
11+
let answer = distances.reduce(0, +) - distances.sorted { $0 > $1 }.prefix(k-1).reduce(0, +)
2912
print(answer)

0 commit comments

Comments
 (0)