Cod sursa(job #2021340)

Utilizator danstefanDamian Dan Stefan danstefan Data 13 septembrie 2017 12:12:50
Problema Zeap Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.73 kb
#include <bits/stdc++.h>
using namespace std;
int x,mi,a;
char c;
map<int,pair<int,int> >mp;
priority_queue<int>q1;
priority_queue<int,vector<int>,greater<int> >q2;
priority_queue<int>q1d;
priority_queue<int,vector<int>,greater<int> >q2d;
int main()
{
    ifstream f ("zeap.in");
    ofstream g ("zeap.out");
    while(f>>c)
        if(c=='I')
        {
            f>>x;
            mp[x].first++;
            mp[x].second++;
            if(mp[x].second==1)
            {
                q1d.push(x);
                q2d.push(x);
            }
            q1.push(x);
            q2.push(x);
        }
        else if(c=='S')
        {
            f>>x;
            if(mp[x].second==0)g<<-1<<'\n';
            else mp[x].second--;
        }
        else if(c=='C')
        {
            f>>x;
            if(mp[x].second==0)g<<0<<'\n';
            else g<<1<<'\n';
        }
        else if(c=='M')
        {
            f>>c;
            if(c=='A')
            {
                f>>c;
                while(mp[q1.top()].first>mp[q1.top()].second&&!q1.empty())mp[q1.top()].first--,q1.pop();
                while(mp[q2.top()].first>mp[q2.top()].second&&!q2.empty())mp[q2.top()].first--,q2.pop();
                if(q1.top()==q2.top()||q1.empty()||q2.empty())g<<-1<<'\n';
                else g<<q1.top()-q2.top()<<'\n';
            }
            else
            {
                f>>c;
                mi=INT_MAX;
                while(mp[q1d.top()].second==0&&!q1d.empty())q1d.pop();
                if(!q1d.empty())a=q1d.top();
                else
                {
                    g<<-1<<'\n';
                    continue;
                }
                q1d.pop();
                while(mp[q1d.top()].second==0&&!q1d.empty())q1d.pop();
                if(!q1d.empty())mi=a-q1d.top(),q1d.push(a);
                else
                {
                    q1d.push(a);
                    g<<-1<<'\n';
                    continue;
                }
                while(mp[q2d.top()].second==0&&!q2d.empty())q2d.pop();
                if(!q2d.empty())a=q2d.top();
                else
                {
                    g<<-1<<'\n';
                    continue;
                }
                q2d.pop();
                while(mp[q2d.top()].second==0&&!q2d.empty())q2d.pop();
                if(!q2d.empty())
                {
                    mi=min(mi,q2d.top()-a),q2d.push(a);
                    g<<mi<<'\n';
                }
                else
                {
                    if(mi!=INT_MAX)g<<mi<<'\n';
                    q2d.push(a);
                    g<<-1<<'\n';
                    continue;
                }
            }
        }
    return 0;
}