728x90
반응형
11279번: 최대 힙
첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가
www.acmicpc.net
풀이
public class BOJ_11279 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(in.readLine());
PriorityQueue<Integer> heap = new PriorityQueue<>(N, new Comparator<Integer>() {
public int compare(Integer i1, Integer i2) {
return i2.compareTo(i1);
}
});
for(int i=0;i<N;i++) {
int num = Integer.parseInt(in.readLine());
if(num == 0) {
if(heap.size() == 0) {
sb.append("0").append("\n");
}else {
sb.append(heap.poll()).append("\n");
}
}else {
heap.add(num);
}
}
System.out.println(sb);
}
}
1927번: 최소 힙
첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0
www.acmicpc.net
풀이
public class BOJ_1927 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(in.readLine());
PriorityQueue<Integer> heap = new PriorityQueue<>();
for(int i=0;i<N;i++) {
int num = Integer.parseInt(in.readLine());
if(num == 0) {
if(heap.size() == 0) {
sb.append("0").append("\n");
}else {
sb.append(heap.poll()).append("\n");
}
}else {
heap.add(num);
}
}
System.out.println(sb);
}
}
728x90
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[문제] 백준 2491번 수열 (0) | 2021.02.18 |
---|---|
[문제] 백준 15686번 치킨 배달 (0) | 2021.02.17 |
[문제] 백준 1476번 날짜 계산 (0) | 2021.02.16 |
[문제] 백준 10972번 다음 순열 (0) | 2021.02.16 |
[문제] 백준 9613번 GCD합 (0) | 2021.02.16 |