ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/1927
// ์ต์๊ฐ์ ๋น ๋ฅด๊ฒ ๋ฝ๋ ๋ฌธ์
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n int
fmt.Fscanln(reader, &n)
var heap Heap
heap.heapArr = make([]uint, n+1)
for i := 0; i < n; i++ {
var x uint
fmt.Fscanln(reader, &x)
if x == 0 {
fmt.Fprintln(writer, heap.deleteHeap())
} else {
heap.insertHeap(x)
}
}
}
type Heap struct {
heapArr []uint
numOfData int
}
func (heap *Heap) deleteHeap() (delVal uint) {
if heap.numOfData == 0 {
return
}
delVal = heap.heapArr[1]
lastVal := heap.heapArr[heap.numOfData]
var parentIdx = 1
var childIdx = heap.getPriorityChildIdx(parentIdx)
for childIdx != -1 {
if lastVal < heap.heapArr[childIdx] {
break
}
heap.heapArr[parentIdx] = heap.heapArr[childIdx]
parentIdx = childIdx
childIdx = heap.getPriorityChildIdx(parentIdx)
}
heap.heapArr[parentIdx] = lastVal
heap.numOfData--
return
}
func (heap *Heap) getPriorityChildIdx(parentIdx int) int {
if parentIdx*2 > heap.numOfData {
return -1
} else if parentIdx*2 == heap.numOfData {
return parentIdx * 2
} else {
if heap.heapArr[parentIdx*2] > heap.heapArr[parentIdx*2+1] {
return parentIdx*2 + 1
}
return parentIdx * 2
}
}
func (heap *Heap) insertHeap(x uint) {
heap.numOfData++
var idx = heap.numOfData
for idx != 1 {
if x < heap.heapArr[idx/2] {
heap.heapArr[idx] = heap.heapArr[idx/2]
idx = idx / 2
} else {
break
}
}
heap.heapArr[idx] = x
}
๐จ Python3
# https://www.acmicpc.net/problem/1927
# ์ต์๊ฐ์ ๋น ๋ฅด๊ฒ ๋ฝ๋ ๋ฌธ์
import sys, heapq
if __name__ == "__main__":
n = int(sys.stdin.readline())
h = []
for i in range(n):
x = int(sys.stdin.readline())
if x == 0:
if len(h) == 0:
print(0)
else:
print(heapq.heappop(h))
else:
heapq.heappush(h, x)
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 14235๋ฒ / ํฌ๋ฆฌ์ค๋ง์ค ์ ๋ฌผ [Python3] (0) | 2020.12.01 |
---|---|
BOJ / 11286๋ฒ / ์ ๋๊ฐ ํ [Go][Python3] (0) | 2020.11.30 |
BOJ / 11279๋ฒ / ์ต๋ ํ [Go][Python3] (0) | 2020.11.28 |
BOJ / 1764๋ฒ / ๋ฃ๋ณด์ก [Go][Python3] (0) | 2020.11.27 |
BOJ / 10815๋ฒ / ์ซ์ ์นด๋ [Go][Python3] (0) | 2020.11.26 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- Macbook pro 2012 mid 13
- python3
- ballet
- ๋ฐ๋
- Algorithm
- ๋ถํ ์ ๋ณต
- ๋ชฝ๊ณ ๋๋น
- ๋งฅ๋ถํ๋ก
- dfs
- BOJ
- ์๊ณ ๋ฆฌ์ฆ
- ์๊ฐ๊ต์ฒด
- ๋ฐฑ์ค
- ์๋ฐ
- ๋ธ๋ฃจํธํฌ์ค
- ํด์๋งต
- ํ
- AWS
- dp
- go
- ํ๋ก์ด๋์์ฌ
- Golang
- ์คํ
- ์ด๋ถํ์
- ๋งฅ๋ถ
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- MongoDB
- baekjoon
- BFS
- java
- Total
- Today
- Yesterday