홈 BOJ 1655 - 가운데를 말해요
글
취소

BOJ 1655 - 가운데를 말해요

백준 1655번: 가운데를 말해요

지금까지 입력된 수를 두 개의 우선순위 큐에 나누어 저장한다. lower는 작은 절반을 담는 최대 힙이고, upper는 큰 절반을 담는 최소 힙이다. 다음 두 불변식을 유지한다.

  • lower의 모든 값은 upper의 모든 값보다 작거나 같다.
  • lower의 원소 수는 upper와 같거나, 정확히 하나 더 많다.

각 수를 값의 범위에 맞는 힙에 넣은 뒤, 필요하면 한 힙의 루트 원소를 다른 힙으로 옮겨 크기 불변식을 복구한다. 그러면 lower의 루트가 항상 아래쪽 중앙값이다. 접두 구간의 길이가 홀수이면 lower에 원소가 하나 더 있으므로 루트가 중앙 원소다. 길이가 짝수이면 두 힙의 크기가 같고, lower의 루트는 가운데 두 값 중 작은 값이므로 문제에서 요구하는 아래쪽 중앙값이다.

모든 입력값을 두 힙에 각각 한 번씩만 저장하므로 공간 복잡도는 O(N)이다. 삽입과 필요할 때의 재균형 이동은 각각 O(log N)이므로 전체 시간 복잡도는 O(N log N)이다. 공백으로 구분된 정수 입력은 버퍼를 사용하는 바이트 리더로 읽는다.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.PriorityQueue;

public class Main {
  private static final class FastScanner {
    private final BufferedInputStream input = new BufferedInputStream(System.in);
    private final byte[] buffer = new byte[1 << 16];
    private int length = 0;
    private int position = 0;

    private int read() throws IOException {
      if (position == length) {
        length = input.read(buffer);
        position = 0;
        if (length == -1) return -1;
      }
      return buffer[position++];
    }

    int nextInt() throws IOException {
      int c;
      do {
        c = read();
      } while (c <= ' ' && c != -1);

      int sign = 1;
      if (c == '-') {
        sign = -1;
        c = read();
      }
      int value = 0;
      while (c > ' ') {
        value = value * 10 + c - '0';
        c = read();
      }
      return value * sign;
    }
  }

  public static void main(String[] args) throws IOException {
    FastScanner scanner = new FastScanner();
    int n = scanner.nextInt();
    PriorityQueue<Integer> lower =
        new PriorityQueue<>(Collections.reverseOrder());
    PriorityQueue<Integer> upper = new PriorityQueue<>();
    StringBuilder output = new StringBuilder();

    for (int i = 0; i < n; i++) {
      int value = scanner.nextInt();
      if (lower.isEmpty() || value <= lower.peek()) {
        lower.add(value);
      } else {
        upper.add(value);
      }

      if (lower.size() > upper.size() + 1) {
        upper.add(lower.poll());
      } else if (upper.size() > lower.size()) {
        lower.add(upper.poll());
      }
      output.append(lower.peek()).append('\n');
    }

    System.out.print(output);
  }
}
이 글은 저자가 CC BY 4.0 라이선스로 배포합니다.