코딩테스트/백준
백준 1927 : 최소 힙 _자바 Java
플래시🦥
2023. 1. 26. 10:46
반응형
https://www.acmicpc.net/problem/1927
1927번: 최소 힙
첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0
www.acmicpc.net
우선순위 큐 사용(오름차순) -> PriorityQueue pq=new PriorityQueue<>();
import java.io.*;
import java.util.*;
import java.time.*;
public class Main {
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine()); //연산의 개수
PriorityQueue<Long> pq=new PriorityQueue<>();
for(int i=0;i<N;i++) { //연산 반복
long val = Integer.parseInt(br.readLine());
if(val>0) {
pq.add(val);
}else if(val==0) {
if(pq.isEmpty())
System.out.println(0);
else
System.out.println(pq.poll());
}
}
}// main()
}// class Main
728x90
반응형