Codeforces Round #357 (Div. 2) -- C. Heap Operations (优先队列模拟)

来源:互联网 发布:php参考手册下载 编辑:程序博客网 时间:2024/06/05 02:52
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 valuex in the heap;
  • getMin x — the value of the minimum element contained in the heap was equal tox;
  • 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 timegetMin 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 ofgetMin 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 eachgetMin operation is equal to the result in the record, and the heap is non-empty whengetMin 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 exceeding109 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 than1 000 000 operations.

Examples
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 is3. To make the result of the first getMin equal to 4 one should firstly remove number3 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 x 把x插入到集合中,removeMin 删除集合中最小的元素,getMin x ,x是集合中的最小值  并把他取出来。给了你n个这样的操作,但可能漏了点什么导致不合法,你需要添加几个操作或者不加使得最终一些列操作都是合理的!

思路:

并不难的题目,模拟一下即可!

集合可以用优先队列,队首始终是最小的元素!

①3个操作中,insert x 肯定是合法的! 直接插进去  把这个过程记录下来(可以用vector 和pair 进行组合!)

②其次比较好写的是removeMin,  直接删除队首元素即可!  但可能会队列是空的,所以需要先插一个数,在删除!

③相对比较麻烦的是getMin x  当队首元素小于x时 要先清空到 x 在记录,如果队列是空或者队首大于x 直接插入x,在删除x即可!

详细逻辑顺序见代码:

#include<cstdio>#include<cstring>#include<cctype>#include<cstdlib>#include<cmath>#include<iostream>#include<sstream>#include<iterator>#include<algorithm>#include<string>#include<vector>#include<set>#include<map>#include<deque>#include<queue>#include<stack>#include<list>typedef long long ll;typedef unsigned long long llu;const int maxn = 100000 + 10;const int inf = 0x3f3f3f3f;const double pi = acos(-1.0);const double eps = 1e-8;using namespace std;priority_queue<int,vector<int>,greater<int> > q;vector<pair<string,int> >res;char s[100];int main(){    int n;    scanf("%d",&n);    for (int i = 0; i < n; ++i){        scanf("%s",s);        int x;        if (s[0] == 'i'){            scanf("%d",&x);            q.push(x);            res.push_back(make_pair("insert",x));            continue;        }        if (s[0] == 'r'){            if (q.empty()){                q.push(1);                res.push_back(make_pair("insert",1));            }            res.push_back(make_pair("removeMin",q.top()));            q.pop();            continue;        }        scanf("%d",&x);        bool ok = false;        while(!q.empty()){            int u = q.top();            if (u < x){                q.pop();                res.push_back(make_pair("removeMin",12));            }            else if (u > x){                res.push_back(make_pair("insert",x));                q.push(x);                res.push_back(make_pair("getMin",x));                ok=true;                break;            }            else {                ok=true;                res.push_back(make_pair("getMin",x));                break;            }        }        if (ok)continue;        if (q.empty()){            //res.push_back();            res.push_back(make_pair("insert",x));            q.push(x);            res.push_back(make_pair("getMin",x));        }    }    int len = res.size();    printf("%d\n",len);    for (int i = 0; i < len; ++i){        printf("%s",res[i].first.c_str());        if (res[i].first[0] =='r'){            printf("\n");            continue;        }        printf(" %d\n",res[i].second);    }    return 0;}


1 0