{% embed url=“https://www.acmicpc.net/problem/11286” %}
Solution 1. 미리 구현된 priorityQueue 자료형 사용
package practice.algorithm.stackqueue;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.PriorityQueue;/** * https://www.acmicpc.net/problem/11286 * 25616 kb / 328 ms
* 절대값 힙 * - priorityQueue 자료형을 사용한다. * */public class Bj11286 { private static final PriorityQueue<Integer> absHeap = new PriorityQueue<>((o1, o2) -> { if (Math.abs(o1) > Math.abs(o2)) { return 1; } if (Math.abs(o1) == Math.abs(o2)) { return o1 - o2; } return -1; }); public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); for(int i = 0; i < n; i++) { int x = Integer.parseInt(br.readLine()); if (x != 0){ absHeap.add(x); continue; } if (absHeap.isEmpty()) { sb.append(0).append("\n"); continue; } sb.append(absHeap.poll()).append("\n"); } System.out.println(sb); }}
Java
복사