A - Heap Operations CodeForces - 681C

原题链接:http://codeforces.com/problemset/problem/681/C

思路:使用stl的priority_queue + 模拟

直接上代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
typedef pair<int, int> P;
priority_queue<int, vector<int>, greater<int> > q;

vector<P> ans;
int main()
{
    ios::sync_with_stdio(false);
    int n, x;
    string s;
    
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>s;
        if(s != "removeMin")
            cin>>x;
        if(s == "insert"){
            ans.push_back(P(1, x));//:insert 2:getmin 3:removemin
            q.push(x);
        }
        else if(s == "removeMin"){
            if(q.empty()){
                q.push(1);
                ans.push_back(P(1, 1));
            }
            q.pop();
            ans.push_back(P(3, -1));
        }
        else if(s == "getMin"){
            if(!q.empty() && x == q.top())
                ans.push_back(P(2, x));
            else if(!q.empty() && x<q.top()){
                ans.push_back(P(1, x));
                q.push(x);
                ans.push_back(P(2, x));
            }else{
                while(!q.empty() && x>q.top()){
                    q.pop();
                    ans.push_back(P(3, -1));
                }
                if((!q.empty() && x<q.top()) || q.empty()){
                    q.push(x);
                    ans.push_back(P(1, x));
                }
                ans.push_back(P(2, x));
            }
        }
    }
    cout<<ans.size()<<endl;
    for(auto it=ans.begin();it!=ans.end();it++){
        if(it->first == 3)
            cout<<"removeMin"<<endl;
        else if(it->first == 1){
            cout<<"insert "<<it->second<<endl;
        }else{
            cout<<"getMin "<<it->second<<endl;
        }
    }
    return 0;
}

当时做的时候,脑子有点糊,debug了好久,把自己绕晕了...

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值