题目地址: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;
}