-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoj_1655.java
More file actions
61 lines (55 loc) · 1.98 KB
/
Boj_1655.java
File metadata and controls
61 lines (55 loc) · 1.98 KB
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
public class Boj_1655 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// PriorityQueue<Integer> pq= new PriorityQueue<>(comp);
// 최소힙
PriorityQueue<Integer> pq_min = new PriorityQueue<>(new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
} // return o1-o2; (오름차순)
});
// 최대힙
PriorityQueue<Integer> pq_max = new PriorityQueue<>(new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
// return o2-o1; (내림차순)
}
});
// 1. 두 개의 pq -> minHeap, maxHeap 선언
// 2. 두 pq가 크기가 같으면 -> maxHeap에 입력된 값 추가 (입력한 값이 minHeap의 peek보다 크다면 둘을 swap)
// 3. 두 pq가 크기가 다르면 -> minHeap에 입력된 값 추가 (입력한 값이 maxHeap의 peek보다 작다면 둘을 swap)
// 4. 최대힙의 peek() < 최소힙의 peek() 성립되어야 함 !! -> 성립X : 최대힙 peek() 과 최소힙 peek() swap!! (각 pool 값을 add)
// 5. maxHeap의 top에 위치한 값이 중간 값 (출력 값)
int N = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
for(int i=0;i<N;i++) {
int num = Integer.parseInt(br.readLine());
if(pq_min.size() == pq_max.size()) {
pq_max.add(num);
} else {
pq_min.add(num);
}
if(!pq_max.isEmpty() && !pq_min.isEmpty()) {
if(pq_max.peek() > pq_min.peek()) { // 최대힙 peek < 최소힙 peek 유지되어야 함!!
pq_max.add(pq_min.poll());
pq_min.add(pq_max.poll());
}
}
sb.append(pq_max.peek() + "\n");
}
System.out.println(sb);
}
// 클래스 재정의
/* public static Comparator<Integer> comp = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1-o2;
}
};
*/
}