๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ฝ”๋”ฉํ…Œ์ŠคํŠธ/TIL

[99ํด๋Ÿฝ] 18์ผ์ฐจ ๋ฌธ์ œ: ํฌ๋ฆฌ์Šค๋งˆ์Šค ์„ ๋ฌผ

by moon101 2025. 2. 12.

 

 

 

 

๋ฐฑ์ค€ ํฌ๋ฆฌ์Šค๋งˆ์Šค ์„ ๋ฌผ ๋ฌธ์ œ๋ฅผ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค. ๋งจ ์ฒ˜์Œ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ ‘ํ–ˆ์„ ๋•Œ ๋˜๊ฒŒ ์–ด๋ ต๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์ด์   ๋งŽ์ด ์ต์ˆ™ํ•ด ์ง„ ๊ฒƒ ๊ฐ™๋‹ค. ์•„์ง ๋น„๊ธฐ๋„ˆ ๋ ˆ๋ฒจ์ด์ง€๋งŒ ๊ณ„์† ํ•˜๋‹ค๋ณด๋ฉด ๋ฆฌํŠธ์ฝ”๋“œ ๋ฏธ๋””์—„์€ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋‚ ์ด ์˜ค๊ฒ ์ง€. 

 

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        
        int n = Integer.parseInt(br.readLine());
        PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
        
        for (int i = 0; i < n; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken());
            
            if (a == 0) {
                if (pq.isEmpty()) {
                    sb.append("-1\n");
                } else {
                    sb.append(pq.poll()).append("\n");
                }
            } else {
                for (int j = 0; j < a; j++) {
                    pq.offer(Integer.parseInt(st.nextToken()));
                }
            }
        }
        
        System.out.print(sb);
    }
}

 

 

๋Œ“๊ธ€