티스토리 뷰

문제


코드

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

public class Problem11297 {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		
		PriorityQueue pq = new PriorityQueue<>(new Comparator() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return o2.compareTo(o1);
			}
		});

		for (int i = 0; i < n; i++) {
			int x = Integer.parseInt(br.readLine());
			if (x == 0) {
				// 배열에서 가장 큰 값을 출력하고 그 값을 배열에서 제거
				if (pq.isEmpty()) {
					System.out.println(0);
				} else {
					System.out.println(pq.poll());
				}
			} else {
				// x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산
				pq.offer(x);
			}
		}
	}
}

출처

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

'알고리즘 > 백준알고리즘(JAVA)' 카테고리의 다른 글

#11286번 : 절대값 힙  (0) 2018.09.15
#11650번 : 좌표 정렬하기  (0) 2018.09.15
#1927번 : 최소 힙  (0) 2018.09.14
#1764번 : 듣보잡  (0) 2018.09.12
#10815번 : 숫자 카드  (0) 2018.09.11
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함