ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

11279๋ฒˆ: ์ตœ๋Œ€ ํž™

์ฒซ์งธ ์ค„์— ์—ฐ์‚ฐ์˜ ๊ฐœ์ˆ˜ N(1≤N≤100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ์—ฐ์‚ฐ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ x๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋งŒ์•ฝ x๊ฐ€ ์ž์—ฐ์ˆ˜๋ผ๋ฉด ๋ฐฐ์—ด์— x๋ผ๋Š” ๊ฐ’์„ ๋„ฃ๋Š”(์ถ”๊ฐ€ํ•˜๋Š”) ์—ฐ์‚ฐ์ด๊ณ , x๊ฐ€ 0์ด

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/11279
// ์ตœ๋Œ“๊ฐ’์„ ๋น ๋ฅด๊ฒŒ ๋ฝ‘๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋ฐฐ์šฐ๋Š” ๋ฌธ์ œ
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
}

// deleteHeap: ํž™์—์„œ ๋ฐ์ดํ„ฐ ์‚ญ์ œ.
// ํž™์˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ๋ฃจํŠธ ๋…ธ๋“œ์˜ ์œ„์น˜์— ์˜ฌ๋ฆฐ ๋‹ค์Œ,
// ์ž์‹ ๋…ธ๋“œ์™€์˜ ๋น„๊ต๊ณผ์ •์„ ๊ฑฐ์น˜๋ฉด์„œ ์ž์‹ ์˜ ์œ„์น˜๋ฅผ ์ฐพ์„ ๋•Œ๊นŒ์ง€ ๋‚ด๋ฆฐ๋‹ค.
func (heap *Heap) deleteHeap() (delVal uint) {
	if heap.numOfData == 0 {
		return
	}
	delVal = heap.heapArr[1]
	lastVal := heap.heapArr[heap.numOfData]
	var parentIdx int = 1 // ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๊ฐ€ ์ €์žฅ๋  ์ธ๋ฑ์Šค
	var childIdx int = 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
}

// getPriorityChildIdx:
// ์ž์‹ ๋…ธ๋“œ ์ค‘ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ์ž์‹์˜ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜.
// ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
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
	}
}

// insertHeap: ํž™์— ๋ฐ์ดํ„ฐ ์ €์žฅ.
// ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์—์„œ๋ถ€ํ„ฐ ๋ถ€๋ชจ ๋…ธ๋“œ์™€ ์šฐ์„ ์ˆœ์œ„ ๋น„๊ต๊ณผ์ •์„ ๊ฑฐ์ณ ์ž์‹ ์˜ ์œ„์น˜๋ฅผ ์ฐพ๋Š”๋‹ค.
func (heap *Heap) insertHeap(x uint) {
	heap.numOfData++
	var idx = heap.numOfData // ์ƒˆ ์š”์†Œ๊ฐ€ ์ €์žฅ๋  ์ธ๋ฑ์Šค ๊ฐ’์„ idx์— ์ €์žฅ

	// ์ƒˆ ์š”์†Œ๊ฐ€ ์ €์žฅ๋  ์œ„์น˜๊ฐ€ ๋ฃจํŠธ ๋…ธ๋“œ์˜ ์œ„์น˜๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ๋ฐ˜๋ณต
	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/11279
# ์ตœ๋Œ“๊ฐ’์„ ๋น ๋ฅด๊ฒŒ ๋ฝ‘๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋ฐฐ์šฐ๋Š” ๋ฌธ์ œ
"""
heapq ๋ชจ๋“ˆ ํ™œ์šฉ:
heapq ๋ชจ๋“ˆ์€ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ๋ฃจํŠธ๋กœ ๊ฐ–๋Š”๋‹ค.
๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋””ํดํŠธ๋กœ ์ œ๊ณตํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ์ตœ์†Œ ํž™์ด๋‹ค.
-> ์ตœ๋Œ€ ํž™์„ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด, ๊ฐ’ ์ž…๋ ฅ ๋˜๋Š” ์ถœ๋ ฅ์‹œ ๋งˆ์ด๋„ˆ์Šค๋ฅผ ๊ณฑํ•˜์—ฌ ๋ถ€ํ˜ธ๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋œ๋‹ค.
"""
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(-1*heapq.heappop(h))
        else:
            heapq.heappush(h, -1*x)
728x90
๋Œ“๊ธ€