알고리즘/문제풀이

[Heap] 최소 힙, 최대 힙

lipnus 2019. 1. 27. 15:27
반응형


백준 11279 최소 힙

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

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.PriorityQueue;

public class Main {

static PriorityQueue<Integer> pq = new PriorityQueue<>();

public static void main(String[] args) throws Exception{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt( br.readLine() );

for(int i=0; i<N ;i++){
int input = Integer.parseInt(br.readLine());

if(input==0){
if(!pq.isEmpty()) System.out.println( pq.poll() );
else System.out.println(0);
}else{
pq.offer(input);
}
}
}
}



백준 11279 최대 힙

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

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.PriorityQueue;

public class Main {

static PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());

public static void main(String[] args) throws Exception{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt( br.readLine() );

for(int i=0; i<N ;i++){
int input = Integer.parseInt(br.readLine());

if(input==0){
if(!pq.isEmpty()) System.out.println( pq.poll() );
else System.out.println(0);
}else{
pq.offer(input);
}
}
}
}


여기만 다르다

static PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());


반응형

'알고리즘 > 문제풀이' 카테고리의 다른 글

[그래프] LCA1  (0) 2019.02.26
[Heap] 절댓값 힙  (0) 2019.01.27
[BFS] 네트워크  (0) 2019.01.20
[다익스트라] 최단경로  (0) 2019.01.20
[DP] 제단  (0) 2019.01.16