C. Heap Operations
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 of getMin 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 removeMin are 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.

Input

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.

Output

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.

Examples
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note

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 4 one 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.

题意:对一个堆有三种操作

1.insert x 插入一个数 x

       2.getMin x 获得最小值x

       3.removeMin 删除最小值 

给你n个一系列的操作(不是完整的操作) 要求你补全操作(使得操作步数尽量小)

题解:优先队列存储  模拟过程

注意

input:

1

removeMin

output:

insert 0

removeMin

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
priority_queue<int, vector<int>, greater<int> > q;
int n;
char s[];
int w;
int a[],b[];
int main()
{
scanf("%d",&n);
int sum=;
memset(a,,sizeof(a));
memset(b,,sizeof(b));
while(!q.empty())
q.pop();
for(int i=;i<=n;i++)
{
scanf("%s",s);
if(s[]=='i')
{
scanf("%d",&w);
a[sum]=;
b[sum++]=w;
q.push(w);
}
if(s[]=='g')
{
scanf("%d",&w);
if(q.empty()||w<q.top())
{
a[sum]=;
b[sum++]=w;
q.push(w);
a[sum]=;
b[sum++]=w;
}
else
{
while(!q.empty()&&w>q.top())
{
a[sum]=;
b[sum++]=;
q.pop();
}
if(q.empty()||w<q.top())
{
a[sum]=;
b[sum++]=w;
q.push(w);
a[sum]=;
b[sum++]=w;
}
else
{
a[sum]=;
b[sum++]=w;
}
}
}
if(s[]=='r')
{
while(q.empty())
{
a[sum]=;
b[sum++]=;
q.push(); }
a[sum]=;
b[sum++]=;
q.pop();
}
}
printf("%d\n",sum-);
for(int i=;i<sum;i++)
{
if(a[i]==)
printf("insert %d\n",b[i]);
if(a[i]==)
printf("getMin %d\n",b[i]);
if(a[i]==)
printf("removeMin\n");
}
return ;
}

最新文章

  1. 重温JSP学习笔记--JSP动作标签
  2. 跟我从零基础学习Unity3D开发--初识U3D
  3. [CareerCup] 18.3 Randomly Generate Integers 随机生成数字
  4. MVC中的Controller
  5. Cube Mapping
  6. mysql 的 infobright 数据库的 mediumblob 显示不了数据
  7. MyEclipse6.6 汉化过程
  8. hdu 2489 最小生成树状态压缩枚举
  9. 声明:function FileSetAttr ( const FileName : string
  10. JMeter IP欺骗压测
  11. matlab-逻辑回归二分类(Logistic Regression)
  12. C++版 - 剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题,ZOJ 1088:System Overload类似)题解
  13. Scrum敏捷开发沉思录
  14. MQ问题
  15. 迭代器模式(java版)
  16. selector的小箭头去除
  17. MT【170】裂项相消
  18. P4810 A’s problem(a)
  19. ESET Smart Security 免费60天
  20. 遍历目录大小——php经典实例

热门文章

  1. Intellij IDEA 像eclipse那样给maven添加依赖,且Intellij idea里在pom.xml里添加Maven依赖,本地仓库下拉列表显示包很少的血的经验
  2. php 操作 mysql 实现批量执行mysql语句 mysql文件
  3. python学习之字符串转换
  4. No module named &#39;PyQt5.sip&#39;
  5. B1081 检查密码 (15分)
  6. POJ:3259-Wormholes(最短路判断负环)
  7. Git-历史穿梭
  8. android 获取图片
  9. Field &#39;flag&#39; doesn&#39;t have a default value错误
  10. H2数据库使用