2 solutions

  • 1
    @ 2022-3-31 21:30:21
    #include<bits/stdc++.h>
    using namespace std;
    int t,n,x;
    char a;
    int main(){
    	std::ios::sync_with_stdio(false);
    	priority_queue<int,vector<int>,greater<int> > num;//小顶堆 
    	cin>>t>>n;
    	while(t--){
    		cin>>a;
    		if(a=='I'){
    			cin>>x;
    			if(num.size()<n)
    				num.push(x);
    			else if(num.top()<x){//当数目大于n是,判断后面的数是否大于队列中最小的数 
    				num.pop();///若小于则没必要加入队列(这样极大的优化了代码)
    				num.push(x);//若大于,则先出队一个元素,再入队,保持队列中只有n个元素 
    			}
    		}else{
    			cout<<num.top()<<endl;
    		}
    	}
    	return 0;
    }
    

    Information

    ID
    333
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    7
    Tags
    # Submissions
    54
    Accepted
    14
    Uploaded By