Codeforces Round #357 (Div. 2) C. Heap Operations(优先队列)

44 篇文章 0 订阅
9 篇文章 0 订阅

题目地址:http://codeforces.com/problemset/problem/681/C

思路:优先队列模拟,注意判断队列为空,若删除时为空,先加入0再删除。

#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#define debu
using namespace std;
const int INF=1e9+1;
int n;
vector<int> Ans;
vector<string> ans;
priority_queue<int,vector<int>,greater<int> > q;
int main()
{
#ifdef debug
    freopen("in.in","r",stdin);
#endif // debug
    ios::sync_with_stdio(0);
    while(cin>>n)
    {
        while(!q.empty()) q.pop();
        for(int i=0; i<n; i++)
        {
            int x;
            char st[15];
            cin>>st;
            if(st[0]=='i')
            {
                cin>>x;
                q.push(x);
                ans.push_back(st);
                Ans.push_back(x);
            }
            else if(st[0]=='r')
            {
                if(q.empty())
                {
                    ans.push_back("insert");
                    Ans.push_back(0);
                    q.push(0);
                }
                q.pop();
                ans.push_back(st);
                Ans.push_back(INF);
            }
            else
            {
                int flag=0;
                cin>>x;
                while(!q.empty())
                {
                    int now=q.top();
                    if(now>x) break;
                    if(now!=x)
                    {
                        q.pop();
                        ans.push_back("removeMin");
                        Ans.push_back(INF);
                    }
                    else
                    {
                        flag=1;
                        ans.push_back(st);
                        Ans.push_back(x);
                        break;
                    }
                }
                if(!flag)
                {
                    ans.push_back("insert");
                    Ans.push_back(x);
                    q.push(x);
                    ans.push_back("getMin");
                    Ans.push_back(x);
                }
            }
        }
        cout<<ans.size()<<endl;
        for(int i=0; i<ans.size(); i++)
            if(Ans[i]==INF) cout<<ans[i]<<endl;
            else
                cout<<ans[i]<<" "<<Ans[i]<<endl;
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值