절댓값 힙

Huisu·2023년 8월 19일
0

Coding Test Practice

목록 보기
29/98
post-thumbnail

문제

https://www.acmicpc.net/problem/11286

문제 설명

절댓값 힙은 다음과 같은 연산을 지원하는 자료구조이다.

  1. 배열에 정수 x (x ≠ 0)를 넣는다.
  2. 배열에서 절댓값이 가장 작은 값을 출력하고, 그 값을 배열에서 제거한다. 절댓값이 가장 작은 값이 여러개일 때는, 가장 작은 수를 출력하고, 그 값을 배열에서 제거한다.

프로그램은 처음에 비어있는 배열에서 시작하게 된다.

제한 사항

첫째 줄에 연산의 개수 N(1≤N≤100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 0이 아니라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0이라면 배열에서 절댓값이 가장 작은 값을 출력하고 그 값을 배열에서 제거하는 경우이다. 입력되는 정수는 -231보다 크고, 231보다 작다.

입출력 예

입력출력
18-1
10
-1-1
0-1
01
01
1-2
12
-10
2
-2
0
0
0
0
0
0
0

입출력 예 설명

힙에서 reHeapup, reHeapdown 수정

아이디어

제출 코드


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class one11286 {
    private int[] heap;
    private int size;

    public void insert(int item) {
        heap[size] = item;
        reHeapUp(size);
        size++;
    }

    public void reHeapUp(int index) {
        while (index > 0) {
            int parentIndex = (index - 1) / 2;
            if (Math.abs(heap[index]) > Math.abs(heap[parentIndex])) break;
            if (Math.abs(heap[index]) == Math.abs(heap[parentIndex]) && heap[index] > heap[parentIndex]) break;
            int temp = heap[index];
            heap[index] = heap[parentIndex];
            heap[parentIndex] = temp;
            index = parentIndex;
        }
    }

    public int remove() {
        int root = heap[0];
        heap[0] = heap[size - 1];
        size--;
        reHeapDown(0);
        return root;
    }

    public void reHeapDown(int index) {
        while (index < size) {
            int leftChild = index * 2 + 1;
            int rightChild = index * 2 + 2;
            int minIndex = index;

            // leftChild > root
            if (leftChild < size &&
                    (Math.abs(heap[leftChild]) < Math.abs(heap[minIndex]) ||
                            (Math.abs(heap[leftChild]) == Math.abs(heap[minIndex]) && heap[leftChild] < heap[minIndex])))
                minIndex = leftChild;

            // rightChild > root
            if (rightChild < size &&
                    (Math.abs(heap[rightChild]) < Math.abs(heap[minIndex]) ||
                            (Math.abs(heap[rightChild]) == Math.abs(heap[minIndex]) && heap[rightChild] < heap[minIndex])))
                minIndex = rightChild;

            if (minIndex == index) break;

            int temp = heap[index];
            heap[index] = heap[minIndex];
            heap[minIndex] = temp;
            index = minIndex;
        }
    }

    public void solution() throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        int[] cal = new int[n];
        int zeroCnt = 0;

        for (int i = 0; i < n; i++) {
            cal[i] = Integer.parseInt(reader.readLine());
            if (cal[i] == 0) zeroCnt++;
        }

        heap = new int[n - zeroCnt];
        size = 0;

        for (int calculation:cal) {
            if (calculation == 0) {
                if (size == 0) System.out.println(0);
                else System.out.println(remove());
            }
            if (calculation != 0) {
                insert(calculation);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        new one11286().solution();
    }
}

0개의 댓글