File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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 , + )
2912print ( answer)
You can’t perform that action at this time.
0 commit comments