Heap Operations 优先队列

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

2insert 3getMin 4

Output

4insert 3removeMininsert 4getMin 4

Input

4insert 1insert 1removeMingetMin 2

Output

6insert 1insert 1removeMinremoveMininsert 2getMin 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[200000],k;
    char b[200000][10];
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%s",b[i]);
        if(strcmp(b[i],yc)==0)
        {
            if(q.empty())
            {
                strcpy(b[i+1],yc);
                strcpy(b[i],cr);
                a[i]=1;
                i++;
                n++;
            }
            else q.pop();
        }
        else
        {
            scanf("%d",&k);
            if(strcmp(b[i],cr)==0)
            {
                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+1],qx);;
                    a[i+1]=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=0;i<n;i++)
    {
        if(strcmp(b[i],yc)==0)printf("%s\n",b[i]);
        else printf("%s %d\n",b[i],a[i]);
    }
}
时间: 2024-10-12 00:10:29

Heap Operations 优先队列的相关文章

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

题意:给定 n 个按顺序的命令,但是可能有的命令不全,让你补全所有的命令,并且要求让总数最少. 析:没什么好说的,直接用优先队列模拟就行,insert,直接放入就行了,removeMin,就得判断一下队列是不是空的,然后再考虑getMin,这个是不是对应的值,如果队列中首元素比它大,那么就加上一个, 如果相等直接取出,如果小于就不断取队列中最小元素. 代码如下: #include <bits/stdc++.h> using namespace std; char s[15], t[30]; v

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

WA * 3 + TLE *1  啊啊啊!说好的考虑问题要仔细呢?! 题意: 简单说就是,有一串操作,丢失了一部分,只有 n 个操作了, 补全操作,使得每次 getMin 得到对应的值.输出所有操作的个数和操作序列. 解题: 用优先队列直接模拟过来的,标记一下某些位置 表示它之前还要进行哪些操作 才能做到,最后直接输出:对于每个情况,如果是 removeMin , 考虑队列是否为空,如果空,就要先插入一个::如果是 getMin ,考虑 当前队列最小的元素 比 给出的数 大还是小,如果小的话要把

CodeForces 681C Heap Operations(模拟)

比较简单的模拟,建议使用STL优先队列. 代码如下: #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; #define N 1000010 char outs[N][20]; int outn[N]; priority_queue<int,vector<int>,greater<int> >q

C++ 优先队列的简单实现

普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有最高级先出 (largest-in,first-out)的行为特征. STL中使用heap实现优先队列,底层容器使用的是vector.这里我们使用一个简单数组来代替vector. 以下是一个简单的优先队列实现方案: template <class T> class priqueue{ public: priqueue(int m){//构

容器适配器之priority_queue

template <class T, class Container = vector<T>,                class Compare = less<typename Container::value_type> > class priority_queue; Priority queuePriority queues are a type of container adaptors, specifically designed such that i

数据结构 二叉堆 &amp; 堆排序

二叉堆,是一个满二叉树,满足堆的性质.即父节点大于等于子节点(max heap)或者是父节点小于等于子节点(min heap).二叉堆的如上性质常用于优先队列(priority queue)或是用于堆排序. 由于max heap 与min heap类似,下文只针对min heap进行讨论和实现. 如上图,是根据字母的ASCII码建立的最小堆. 我们用数组对满二叉树采用宽度优先遍历存储堆结构,如下图所示: 从数组下标1开始存储堆,这样的处理方式可以得到如下性质: 1.堆中的每个父节点k,他的两个子

ACM-ICPC 2017 Asia HongKong 解题报告

ACM-ICPC 2017 Asia HongKong 解题报告 任意门:https://nanti.jisuanke.com/?kw=ACM-ICPC%202017%20Asia%20HongKong 按AC次序: D - Card collection In an online game, a player can collect different types of power cards. Each power card can enable a player to have a uni

Looking deeper into SQL Server using Minidumps

https://blogs.msdn.microsoft.com/sqlcat/2009/09/11/looking-deeper-into-sql-server-using-minidumps/ Author: Thomas Kejser Reviewers and Contributors: Bob Ward, Michael Thomassy, Juergen Thomas, Hermann Daeubler, Mark Souza, Lubor Kollar, Henk van der

【Go语言】集合与文件操作

本文目录 1.数据集合的主要操作 1_1.字典的声明 1_2.字典的初始化和创建 1_3.字典的访问和操作 1_4.其他类型的数据集 2.文件操作 2_1.文件操作概述os包和path包 2_2.文件操作示例 目录操作: 打开与建立文件: 写文件 : 读文件: 删除文件: 回到顶部 1.集合以及主要操作 首先要提到的是Go语言的内置数据类型map(字典类型 类似于Java中的HashMap和Swift中的Directory),这样你无需导入任何包便可 使用map类型了.map是一种特殊的数据结构