2022. 10. 24. 11:07γAlgorithm
λ°±μ€ - μ μ μ¬μ(Swift)
λ¬Έμ μ€λͺ
https://www.acmicpc.net/problem/1946
λμ νμ΄
μλ₯μνμ΄λ λ©΄μ μν λ μ€ νλμ κΈ°μ€μΌλ‘ λ¨Όμ μ λ ¬μ μν€λλ° μ΄λ κ² νλ©΄ λ€μμλ κ°μ νμ μμ μλ κ°λ³΄λ€ 1κ° μ΄μ λ€μ³μ§κΈ° λλ¬Έμ μΉ΄μ΄νΈκ°λλ €λ©΄ μμ μλ κ°μ λλ¨Έμ§ κ°(μ λ ¬λμ§ μμ κ°)λ³΄λ€ λμμΌνλ€.
μ£Όμν μ μ λλ¨Έμ§ κ°μ μ§νλ λλ§λ€ μμκ° λμμ§λ©΄ κ°μ₯ λμ μμλ₯Ό κ°±μ μν€κ³ μ§ν ν΄μΌνλ€λ κ²μ΄λ€.
κ·Έλ¦¬κ³ μ΄ λ¬Έμ λ₯Ό νλ©΄μ readLineμΌλ‘λ ν μ μλ€λ κ²λ μκ²λμλ€. readLineμ ν μ€μ© μ½κ²λλλ° μ΄λ μ λ ₯κ°μ΄ 100,000κ°κ° λμ΄κ°λ©΄ μκ°μ΄κ³Όκ° μΌμ΄λ¬λ€.
κ·Έλμ λμμΌλ‘ λΌμ΄λ Έλμ FileIOλ₯Ό μ΄μ©ν΄μ λ²νΌλ₯Ό μ΄μ©ν΄μ λ¬Έμ λ₯Ό ν΄κ²°ν΄μΌνλ€.
λ²νΌλ CPUμ μ μΆλ ₯μ₯μΉκ°μ μ°¨μ΄λ₯Ό μ΅μνμν€κΈ°μν΄μ μ¬μ©νλ μμμ μΈ κ³΅κ°μ΄λ©°, CPUμμ μ μΆλ ₯μ₯μΉμ μꡬλ₯Ό μ¦κ°μ μΌλ‘ νΈμΆνλ κ²λ³΄λ€ λ²νΌμμ λ°μ΄ν°λ₯Ό λͺ¨μλ€κ° νλ²μ μ²λ¦¬νλ κ²μ΄ ν¨μ¨μ μ΄κΈ° λλ¬Έμ λ²νΌλ₯Ό μ¬μ©νλ κ²μΌλ‘ μκ³ μλ€. (μ ννμ§λ μμ΅λλ€π§)
import Foundation
// λΌμ΄λ
Έλμ FileIO
final class FileIO {
private var buffer:[UInt8]
private var index: Int
init(fileHandle: FileHandle = FileHandle.standardInput) {
buffer = Array(fileHandle.readDataToEndOfFile())+[UInt8(0)] // μΈλ±μ€ λ²μ λμ΄κ°λ κ² λ°©μ§
index = 0
}
@inline(__always) private func read() -> UInt8 {
defer { index += 1 }
return buffer.withUnsafeBufferPointer { $0[index] }
}
@inline(__always) func readInt() -> Int {
var sum = 0
var now = read()
var isPositive = true
while now == 10
|| now == 32 { now = read() } // 곡백과 μ€λ°κΏ 무μ
if now == 45{ isPositive.toggle(); now = read() } // μμ μ²λ¦¬
while now >= 48, now <= 57 {
sum = sum * 10 + Int(now-48)
now = read()
}
return sum * (isPositive ? 1:-1)
}
@inline(__always) func readString() -> String {
var str = ""
var now = read()
while now == 10
|| now == 32 { now = read() } // 곡백과 μ€λ°κΏ 무μ
while now != 10
&& now != 32 && now != 0 {
str += String(bytes: [now], encoding: .ascii)!
now = read()
}
return str
}
}
let file = FileIO()
let tc = file.readInt()
for _ in 0..<tc {
var arr: [(Int, Int)] = []
var cnt = 1
let n = file.readInt()
for _ in 0..<n {
arr.append((file.readInt(), file.readInt()))
}
arr.sort(by: {$0.1 < $1.1})
var min = arr[0].0
for i in 1..<arr.count {
if arr[i].0 < min {
min = arr[i].0
cnt += 1
}
}
print(cnt)
}
'Algorithm' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
λ°±μ€ - λ€μ§κΈ°(Swift) (0) | 2022.10.24 |
---|---|
λ°±μ€ - μΉ΄λ μ λ ¬νκΈ°(Swift) (0) | 2022.10.24 |
λ°±μ€ - 30(Swift) (0) | 2022.10.24 |
λ°±μ€ - μ£Όμ μ(Swift) (0) | 2022.10.24 |
λ°±μ€ - μλ€μ ν©(Swift) (0) | 2022.10.23 |