-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path11286번.swift
89 lines (81 loc) · 2.43 KB
/
11286번.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
// 출처 : 백준 절댓값 힙
// https://www.acmicpc.net/problem/11286
// 풀이 : hogumachu
import Foundation
func solution() {
let n = Int(readLine()!)!
var absHeap = Heap()
for _ in 0..<n {
let value = Int(readLine()!)!
if value == 0 {
if absHeap.isEmpty() {
print(0)
} else {
print(absHeap.remove()!)
}
} else {
absHeap.add(value)
}
}
}
solution()
struct Heap {
var array: [Int] = []
var compare : (Int, Int) -> Bool = {
if abs($0) == abs($1) {
return $0 > $1
} else {
return abs($0) > abs($1)
}
}
mutating func add(_ x: Int) {
array.append(x)
var index = array.count - 1
while index > 0, compare(array[(index-1)/2], array[index]) {
array.swapAt((index-1)/2, index)
index = (index-1)/2
}
}
mutating func remove() -> Int? {
if array.isEmpty { return nil }
array.swapAt(0, array.count - 1)
let returnValue = array.removeLast()
var index = 0
while index < array.count {
let compareIndex = index*2+1
var changed = false
if compareIndex+1 < array.count {
if compare(array[index], array[compareIndex]) {
if compare(array[compareIndex], array[compareIndex+1]) {
array.swapAt(index, compareIndex+1)
index = compareIndex+1
changed = true
} else {
array.swapAt(index, compareIndex)
index = compareIndex
changed = true
}
} else if compare(array[index], array[compareIndex+1]) {
array.swapAt(index, compareIndex+1)
index = compareIndex+1
changed = true
}
} else if compareIndex < array.count {
if compare(array[index], array[compareIndex]) {
array.swapAt(index, compareIndex)
index = compareIndex
changed = true
}
} else {
break
}
if !changed {
break
}
}
return returnValue
}
func isEmpty() -> Bool {
return array.isEmpty
}
}