Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
- put the given number into the heap;
- get the value of the minimum element in the heap;
- extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
- insert x — put the element with value x in the heap;
- getMin x — the value of the minimum element contained in the heap was equal to x;
- removeMin — the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some ofgetMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMinare applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
The first line of the input contains the only integer n (1 ≤ n ≤ 100 000) — the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
The first line of the output should contain a single integer m — the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
2 insert 3 getMin 4
4 insert 3 removeMin insert 4 getMin 4
4 insert 1 insert 1 removeMin getMin 2
6 insert 1 insert 1 removeMin removeMin insert 2 getMin 2
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice.
这道题让我真正的学到了之前了解过的优先队列,以前只是知道有优先队列这个东西,这道题才是让我真正的懂了什么叫优先队列。
priority_queue<int>q;这是普通的优先队列,数据大的优先级高,也就是q.top()会得到最大的数
priority_queue<int,vector<int>,greater<int> >q;这是改造过的优先队列,数据小的优先级高,也就是q.top()会得到最小的数
prio_queue<node>q;这是一种自定义的优先队列,但是必须要重载<符号
struct node
{
friend bool operator< (node n1, node n2)
{
return n1.priority < n2.priority;
}
int priority;
int value;
};
优先队列常用的函数
q.push(x); 向优先队列添加一个元素
q.pop(); 删除优先队列优先级最高的元素
q.top(); 返回优先队列优先级最高的元素
q.size(); 返回优先队列元素个数
q.empty(); 如果优先队列为空,返回真
另外在说一下这道题的注意点,当队列为空时想要removeMin或者getMin得先insert一个数再处理,队列中有比getMin的数小的数时要先把小的数全部removeMin再处理。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
using namespace std;
priority_queue<int,vector<int>,greater<int> >q;
int f[1000050];
string s[1000050];
int main()
{
ios::sync_with_stdio(false);
int n,num=0;
scanf("%d",&n);
while(n--)
{
char a[10];
int x;
scanf("%s",a);
if(a[0]=='i')
{
scanf("%d",&x);
q.push(x);
s[num]="insert";
f[num++]=x;
}
if(a[0]=='g')
{
scanf("%d",&x);
while(!q.empty()&&q.top()<x)
{
q.pop();
s[num++]="removeMin";
}
if(q.empty()||q.top()>x)
{
q.push(x);
s[num]="insert";
f[num++]=x;
}
s[num]="getMin";
f[num++]=x;
}
if(a[0]=='r')
{
if(q.empty())
{
q.push(0);
s[num]="insert";
f[num++]=0;
}
s[num++]="removeMin";
q.pop();
}
}
cout<<num<<endl;
for(int i=0;i<num;i++)
{
cout<<s[i];
if(s[i][0]!='r')
cout<<" "<<f[i];
cout<<endl;
}
return 0;
}