11279번: 최대 힙
첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0
www.acmicpc.net
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception{
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
PriorityQueue<Integer> pq=new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
return o2-o1;
}
});
for(int i=0;i<N;i++) {
int num=sc.nextInt();
if(num==0) {
printNum(pq);
}
else pq.offer(num);
}
}
static void printNum(PriorityQueue<Integer> pq) {
if(pq.isEmpty()) System.out.println(0);
else System.out.println(pq.poll());
}
}
'코테 공부' 카테고리의 다른 글
[TreeMap]파일정리(백준 20291번, 자바)☆ (0) | 2023.01.26 |
---|---|
[우선순위큐]절댓값 힙(백준 11286번, 자바) (0) | 2023.01.26 |
[우선순위 큐] 중간 값 구하기(백준 2696번, 자바)☆ (0) | 2023.01.25 |
파이프 옮기기(백준 17070번, 자바) (0) | 2023.01.15 |
[DFS]주사위 굴리기2(23288번, 자바) (0) | 2023.01.12 |