import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
public class Madin {
public static class MedianHolder {
private PriorityQueue<Integer> maxHeap =
new PriorityQueue<Integer>(new MaxHeapComparator());
private PriorityQueue<Integer> minHeap =
new PriorityQueue<Integer>(new MinHeapComparator());
private void modifyTwoHeapSize() {
if (this.maxHeap.size() == this.minHeap.size() + 2) {
this.minHeap.add(this.maxHeap.poll());
}
if (this.minHeap.size() == this.maxHeap.size() + 2) {
this.maxHeap.add(this.minHeap.poll());
}
}
public void addNum(int num) {
if (maxHeap.isEmpty() || num < maxHeap.peek()) {
maxHeap.add(num);
} else {
minHeap.add(num);
}
modifyTwoHeapSize();
}
public Integer getMedian() {
int maxHeapSize = this.maxHeap.size();
int minHeapSize = this.minHeap.size();
if (maxHeapSize + minHeapSize == 0) {
return null;
}
Integer maxHeapHead = this.maxHeap.peek();
Integer minHeapHead = this.minHeap.peek();
if (((maxHeapSize + minHeapSize) & 1) == 0) {
return (maxHeapHead + minHeapHead) >> 1;
}
return maxHeapSize > minHeapSize ? maxHeapHead : minHeapHead;
}
}
public static class MaxHeapComparator implements Comparator<Integer> {
public int compare(Integer a, Integer b) {
if (b > a)
return 1;
return -1;
}
}
public static class MinHeapComparator implements Comparator<Integer> {
public int compare(Integer a, Integer b) {
if (b < a)
return 1;
return -1;
}
}
}