Dev.baelanche

[백준 11279] 최대 힙 본문

Data Structure & Algorithm/PS - JAVA

[백준 11279] 최대 힙

baelanche 2019. 5. 21. 21:08
반응형

 

 

최대힙을 구현하여 문제에 맞춰 동작하면 된다.

 

 

public class Main {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        MaxHeap h = new MaxHeap(100001);
        int n = sc.nextInt();
        for(int i=0; i<n; i++) {
            int x = sc.nextInt();
            if(x == 0)
                System.out.println(h.delete());
            else
                h.insert(x);
        }
    }
}

class MaxHeap {
    
    int heap[];
    int size;
    
    public MaxHeap(int size) {
        heap = new int[size];
    }
    
    public void insert(int n) {
        heap[++size] = n;
        for(int i=size; i>1; i/=2) {
            if(heap[i/2] < heap[i])
                swap(i/2, i);
            else break;
        }
    }
    
    public int delete() {
        if(size == 0) return 0;
        
        int root = heap[1];
        heap[1] = heap[size];
        size--;
        
        for(int i=1; i*2<=size;) {
            if(heap[i] > heap[i*2] && heap[i] > heap[i*2+1]) break;
            else if(heap[i*2] > heap[i*2+1]) {
                swap(i, i*2);
                i = i*2;
            } else {
                swap(i, i*2+1);
                i = i*2+1;
            }
        }
        return root;
    }
    
    public void swap(int a, int b) {
        int temp = heap[a];
        heap[a] = heap[b];
        heap[b] = temp;
    }
}
반응형

'Data Structure & Algorithm > PS - JAVA' 카테고리의 다른 글

[백준 2220] 힙 정렬  (0) 2019.05.21
[백준 1927] 최소 힙  (0) 2019.05.21
[백준 4963] 섬의 개수  (0) 2019.05.21
[백준 10989] 수 정렬하기 3  (0) 2019.05.18
[백준 2751] 수 정렬하기 2  (0) 2019.05.18
Comments