大佬们为什么老是显示没有输入输出呢 随时随地找到中位数的题

import java.util.*;


public class MedianHolder {


    public static class MedHolder{
        // PriorityQueue结构就是堆
        private PriorityQueue<Long> maxHeap = new PriorityQueue<Long>(Collections.reverseOrder());
        private PriorityQueue<Long> minHeap = new PriorityQueue<Long>();

        private void modifyTwoHeaps(){
            if (maxHeap.size() == minHeap.size() + 2){
                minHeap.add(maxHeap.poll());
            }
            if (minHeap.size() == maxHeap.size() + 2){
                maxHeap.add(minHeap.poll());
            }
        }

        // peek()获取堆顶的元素,但是不删除
        public void addNumber(Long num){
            if (maxHeap.isEmpty() || num <= maxHeap.peek()){
                maxHeap.add(num);
            }else {
                minHeap.add(num);
            }
            modifyTwoHeaps();
        }

        public double getMedian(){
            if (maxHeap.isEmpty()){
                return -1;
            }
            if (maxHeap.size() == minHeap.size()){
                return (maxHeap.peek() + minHeap.peek()) / 2.0;
            }else {
                return maxHeap.size() > minHeap.size() ? (double)maxHeap.peek() : (double)minHeap.peek();
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int nums = sc.nextInt();
        ArrayList<Double> al = new ArrayList<>();
        MedHolder medHolder = new MedHolder();
        for (int i = 0; i < nums; i++) {
            int num = sc.nextInt();
            if (num == 1){
                long j = sc.nextLong();
                medHolder.addNumber(j);
            }else{
                al.add(medHolder.getMedian());
            }
        }
        for (Double a : al) {
            System.out.println(a);
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务