You are viewing a single comment's thread. Return to all comments →
A c++ solution using 2 heaps. 1 to track the overall min item and 1 to track the min of the removed items.
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <queue> using namespace std; priority_queue<int, vector<int>, greater<int> > pq; priority_queue<int, vector<int>, greater<int> > pqRemove; void add(){ int n; cin>>n; pq.push(n); } void remove() { int n; cin>>n; pqRemove.push(n); } void printMin(){ while(!pqRemove.empty() && pqRemove.top() == pq.top()){ pq.pop(); pqRemove.pop(); } cout<<pq.top()<<endl; } int main() { int q; cin>>q; while(q!=0){ int command; cin>>command; if(command == 1) add(); else if(command == 2) remove(); else if(command == 3) printMin(); q--; } return 0; }
Seems like cookies are disabled on this browser, please enable them to open this website
QHEAP1
You are viewing a single comment's thread. Return to all comments →
A c++ solution using 2 heaps. 1 to track the overall min item and 1 to track the min of the removed items.