1 条题解

  • 0
    @ 2025-5-8 21:30:59

    JAVA AC代码:

    import java.util.*;
    import java.util.StringTokenizer;
    import java.io.*;
    
    public class Main {
    
    	public static void main(String[] args) {
    		int n=in.nextInt();
    		PriorityQueue<Integer> q=new PriorityQueue<>((o1,o2)->Integer.compare(o2, o1));//实现大的在前面
    		Map<Integer,Integer> map=new HashMap<>();//记录每个数有多少个
    		while(n-->0){
    			int op=in.nextInt();
    			if(op==1) {
    				int x=in.nextInt();
    				q.add(x);
    				map.put(x,map.getOrDefault(x, 0)+1);
    			}else if(op==2) {
    				int x=in.nextInt();
    				out.println(map.getOrDefault(x, 0));
    			}else if(op==3) {
    				int x=in.nextInt();
    				if(q.contains(x)) {
    					q.remove(x);
    					map.put(x,map.get(x)-1);
    				}
    			}else if(op==4) {
    				int x=in.nextInt();
    				while(q.contains(x)) {
    					q.remove(x);
    				}
    				map.remove(x);
    			}else if(op==5) {
    				out.println(q.size());
    			}else if(op==6) {
    				out.println(q.peek());
    			}else {
    				if(!q.isEmpty()) {
    					int x=q.poll();
    					map.put(x, map.get(x)-1);
    			}}
    		}
    		out.close();
    	}
    	
    	
    
    	static FastReader in=new FastReader();
    	static PrintWriter out=new PrintWriter(System.out);
    	static class FastReader{
    		static BufferedReader br;
    		static StringTokenizer st;
    		FastReader(){
    			br=new BufferedReader(new InputStreamReader(System.in));
    		}
    		String next() {
    			String str="";
    			while(st==null||!st.hasMoreElements()) {
    				try {
    					str=br.readLine();
    				}catch(IOException e) {
    					throw new RuntimeException(e);
    				}
    				st=new StringTokenizer(str);
    			}
    			return st.nextToken();
    		}
    		int nextInt() {
    			return Integer.parseInt(next());
    		}
    		double nextDouble() {
    			return Double.parseDouble(next());
    		}
    		long nextLong() {
    			return Long.parseLong(next());
    		}
    	}
    	
    }
    
    
    • 1

    信息

    ID
    29
    时间
    1000ms
    内存
    256MiB
    难度
    4
    标签
    递交数
    34
    已通过
    13
    上传者