题目中要求在每个2操作删除最小的一些元素,因此考虑使用小根堆来维护。
具体地,在每个1操作直接将 放入堆中,在每个2操作不断删除堆顶元素直到堆顶元素超过 为止。
代码:
#include<bits/stdc++.h>
using namespace std;
int Q,op,h;
priority_queue<int,vector<int>,greater<int> >q;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>Q;
while(Q--){
cin>>op>>h;
if(op==1){
q.push(h);
}
if(op==2){
if(q.size()){
while(q.top()<=h){
q.pop();
if(q.empty())break;
}
}
}
cout<<q.size()<<'\n';
}
return 0;
}