CMG - Collecting Mango - SPOJ Solution C++

  Problem Link : CMG 


👉 Hint : Use stack and some sorted DS like multiset in C++ STL

 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    multiset<int,greater<int> > v;
    int t;
    cin>>t;
    for(int p=1;p<=t;p++)
    {
        stack<int> s;
        v.clear();
        int n,x;
        string c;
        cin>>n;
        cout<<"Case "<<p<<":"<<"\n";
        for(int i=0;i<n;i++)
        {
            cin>>c;
            if(c=="A")
            {
                cin>>x;
                v.insert(x);
                s.push(x);
            }
            else if(c=="R")
            {
                if(v.size()==0)
                    continue;
                x=s.top();
                s.pop();
                v.erase(v.find(x));
            }
            else
            {
                if(v.size()==0)
                    cout<<"Empty"<<"\n";
                else
                	cout<<*(v.begin())<<"\n";
            }
        }
    }
} 

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!