-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboj_01655.swift
139 lines (107 loc) · 3.5 KB
/
boj_01655.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// 2022.04.25
// Dongyoung Kwon @Chuncheonian (ehddud2468@gmail.com)
// https://www.acmicpc.net/problem/1655
import Foundation
let fIO = FileIO()
var maxHeap = Heap<Int>(priorityFunction: >)
var minHeap = Heap<Int>(priorityFunction: <)
var answer: String = ""
for i in 1...fIO.readInt() {
let new = fIO.readInt()
if i % 2 == 1 {
maxHeap.insert(new)
} else {
minHeap.insert(new)
}
if maxHeap.isEmpty == false, minHeap.isEmpty == false, maxHeap.peek! > minHeap.peek! {
let a = maxHeap.remove()!
let b = minHeap.remove()!
maxHeap.insert(b)
minHeap.insert(a)
}
answer += "\(maxHeap.peek!)\n"
}
print(answer)
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)
}
}
struct Heap<T: Comparable> {
private var elements: [T] = []
private let priorityFunction: (T, T) -> Bool
var isEmpty: Bool {
return elements.isEmpty
}
var count: Int {
return elements.count
}
var peek: T? {
return elements.first
}
init(elements: [T] = [], priorityFunction: @escaping (T, T) -> Bool) {
self.elements = elements
self.priorityFunction = priorityFunction
}
func leftChildIndex(of index: Int) -> Int {
return 2 * index + 1
}
func rightChildIndex(of index: Int) -> Int {
return 2 * index + 2
}
func parentIndex(of index: Int) -> Int {
return (index - 1) / 2
}
mutating func insert(_ node: T) {
elements.append(node)
swimUp(from: elements.endIndex - 1)
}
mutating func swimUp(from index: Int) {
var index = index
while index > 0, priorityFunction(elements[index], elements[parentIndex(of: index)]) {
elements.swapAt(index, parentIndex(of: index))
index = parentIndex(of: index)
}
}
mutating func remove() -> T? {
if elements.isEmpty { return nil }
elements.swapAt(0, elements.endIndex - 1)
let deleted = elements.removeLast()
diveDown(from: 0)
return deleted
}
mutating func diveDown(from index: Int) {
var higherPriority: Int = index
let leftChildIndex: Int = leftChildIndex(of: index)
let rightChildIndex: Int = rightChildIndex(of: index)
if leftChildIndex < elements.endIndex, priorityFunction(elements[leftChildIndex], elements[higherPriority]) {
higherPriority = leftChildIndex
}
if rightChildIndex < elements.endIndex, priorityFunction(elements[rightChildIndex], elements[higherPriority]) {
higherPriority = rightChildIndex
}
if higherPriority == index { return }
elements.swapAt(higherPriority, index)
diveDown(from: higherPriority)
}
}