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.

Example

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.

题意就是模仿一个堆操作的步骤,少了几步,要让添加最少的操作 让操作正常运行。用到优先队列,不停的读,当读到一个insert操作直接将数入队就好了,没啥毛病,如果遇到了removeMin,需要判断这样的情况队列不空,那就正常,如果队列空了肯定不能正常运行,就要随便插入一个数(在这个操作之前),然后再removeMin,如果遇到的是getMin操作,有点麻烦,代码写的很详细,看代码吧,还有就是别用输入输出流,时间差的太大了 超时,换成了printf瞬间100ms。

代码:

#include <iostream>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#define cr "insert"
#define yc "removeMin"
#define qx "getMin"
using namespace std;
class que
{
public:
int d;
friend bool operator <(const que a,const que b)
{
return a.d>b.d;
}
}temp;
int main()
{
priority_queue<que> q;
int n,*a=new int[],k;
char b[][];
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%s",b[i]);
if(strcmp(b[i],yc)==)
{
if(q.empty())
{
strcpy(b[i+],yc);
strcpy(b[i],cr);
a[i]=;
i++;
n++;
}
else q.pop();
}
else
{
scanf("%d",&k);
if(strcmp(b[i],cr)==)
{
a[i]=k;
temp.d=k;
q.push(temp);
}
else
{
if(!q.empty()&&q.top().d<k)
{
while(!q.empty()&&q.top().d<k)
{
strcpy(b[i],yc);
q.pop();
i++;
n++;
}
}
if(q.empty()||q.top().d>k)
{
strcpy(b[i+],qx);;
a[i+]=k;
strcpy(b[i],cr);
a[i]=k;
i++;
n++;
temp.d=k;
q.push(temp);
}
strcpy(b[i],qx);
a[i]=k;
}
}
}
printf("%d\n",n);
for(int i=;i<n;i++)
{
if(strcmp(b[i],yc)==)printf("%s\n",b[i]);
else printf("%s %d\n",b[i],a[i]);
}
}

Heap Operations 优先队列的更多相关文章

  1. Codeforces 681C. Heap Operations 优先队列

    C. Heap Operations time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

  2. Heap Operations(模拟题)

     Heap Operations time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  3. Codeforces Round #357 (Div. 2) C. Heap Operations 模拟

    C. Heap Operations 题目连接: http://www.codeforces.com/contest/681/problem/C Description Petya has recen ...

  4. CodeForces 681C Heap Operations (模拟题,优先队列)

    题意:给定 n 个按顺序的命令,但是可能有的命令不全,让你补全所有的命令,并且要求让总数最少. 析:没什么好说的,直接用优先队列模拟就行,insert,直接放入就行了,removeMin,就得判断一下 ...

  5. CodeForces 681C Heap Operations(模拟)

    比较简单的模拟,建议使用STL优先队列. 代码如下: #include<iostream> #include<cstdio> #include<cstring> # ...

  6. Codeforces Round #357 (Div. 2)C. Heap Operations

    用单调队列(从小到大),模拟一下就好了,主要是getMin比较麻烦,算了,都是模拟....也没什么好说的.. #include<cstdio> #include<map> #i ...

  7. 二叉堆(binary heap)—— 优先队列的实现

    二叉堆因为对应着一棵完全二叉树,因而可以通过线性数组的方式实现. 注意,数组第 0 个位置上的元素,作为根,还是第 1 个位置上的元素作为根? 本文给出的实现,以数组第 1 个位置上的元素作为根,则其 ...

  8. Codeforces Round #357 (Div. 2) 优先队列+模拟

    C. Heap Operations time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. GO语言heap剖析及利用heap实现优先级队列

    GO语言heap剖析 本节内容 heap使用 heap提供的方法 heap源码剖析 利用heap实现优先级队列 1. heap使用 在go语言的标准库container中,实现了三中数据类型:heap ...

随机推荐

  1. 微服务API网关

    当你选择采用微服务构建自己的程序,则你需要考虑客户端怎样与后端服务交互.对于一个单体应用,仅有一个服务群提供服务(通过负载均衡器实现).在微服务架构里面,每一个服务都暴漏了一个服务器集群.本篇文章我们 ...

  2. [转]mysql-mmm集群(多实例)

    一.需求说明 最近一直在学习mysql-mmm,想以后这个架构也能用在我们公司的业务上,我们公司的业务是单机多实例部署,所以也想把mysql-mmm部署成这样,功夫不负有心人,我成功了,和大家分享一下 ...

  3. jsp动作之 forward

    forward说明了,就想当于php的include,require函数.(但是它是跳转.forward之前的数据都不会显示) 这么说你明白了吗.就是包含,说的好听点就是跳转,但是url地址栏却是没有 ...

  4. mysql数据库切分

    一.数据的垂直切分概念:数据的垂直切分,也可以称之为纵向切分.将不同的表分散到不同的数据库主机中.一个应用系统,总体功能肯定是由很多个功能模块所组成的,而每一个功能模块所需要的数据对应到数据库中就是一 ...

  5. LeetCode--172--阶乘后的0

    问题描述: 给定一个整数 n,返回 n! 结果尾数中零的数量. 示例 1: 输入: 3 输出: 0 解释: 3! = 6, 尾数中没有零. 示例 2: 输入: 5 输出: 1 解释: 5! = 120 ...

  6. python-day36--并发编程之多线程

    十三.死锁.递归锁 1.所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永 ...

  7. HDOJ1003

    #include<iostream> using namespace std; int main() { ],t=,m; cin >> n; while(n--) { cin ...

  8. Activiti的后台数据库表详解

    Activiti的后台是有数据库的支持,所有的表都以ACT_开头. 第二部分是表示表的用途的两个字母标识.用途也和服务的API对应. 1)       ACT_RE_*: 'RE'表示reposito ...

  9. Vim:replace with foobar (y/n/a/q/l/^E/^Y)?

    y:to substitute this match n:to skip this match a:to substitute this and all remaining matches q:to ...

  10. 【转】在SQL Server中创建用户角色及授权(使用SQL语句)

    1. 首先在 SQL Server 服务器级别,创建登陆帐户(create login) --创建登陆帐户(create login) create login dba with password=' ...