C

来源:互联网 发布:淘宝店铺名怎么修改 编辑:程序博客网 时间:2024/06/07 07:02

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

思路:分三种情况:
1. 如果为insert ,直接入堆即可;
2. 如果为removeMin,就判断是否为空,如果为空,先加上一个insert,再进行removeMin;如果不为空,直接removeMin
3. 如果是getMin,如果不为空,就判断是否输入的t,t<堆顶的元素,就一直pop(),直到t<=堆顶的元素;如果t=堆顶的元素,就直接getMin即可;如果t>堆顶的元素,就加一个insert t即可;如果为空,直接insert t即可
此题为优先队列的模拟

#include<cstdio>#include<iostream>#include<queue>#include<cstring>using namespace std;struct node{    int x;    char a[20];}s[1000000];int main (){    int n,i=0,t;    char a[20];    scanf("%d",&n);    priority_queue<int,vector<int>,greater<int> >q;    while(n--)    {    cin>>a;    if(a[0]=='i')    {        scanf("%d",&t);        s[i].x=t;        q.push(t);        strcpy(s[i++].a,a);    }    else if(a[0]=='r')    {        if(q.size()==0)        {            s[i].x=1;            q.push(1);            strcpy(s[i++].a,"insert");        }        q.pop();        strcpy(s[i++].a,a);    }    else    {        scanf("%d",&t);    if(q.size()!=0&&t==q.top())            {                s[i].x=t;                strcpy(s[i++].a,a);            }            else if(q.size()!=0&&q.top()>t)            {                s[i].x=t;                strcpy(s[i++].a,"insert");                q.push(t);                s[i].x=t;                strcpy(s[i++].a,"getMin");            }            else            {                while(q.size()!=0&&q.top()<t)                {                    q.pop();                    strcpy(s[i++].a,"removeMin");                }                if(q.size()!=0&&q.top()>t||q.size()==0)                {                    q.push(t);                    s[i].x =t;                    strcpy(s[i++].a,"insert");                }                s[i].x =t;                strcpy(s[i++].a,"getMin");            }    }    }      printf("%d\n",i);      for(int j=0;j<i;j++)      {        if(s[j].a[0]=='r')        printf("removeMin\n");        else        {            if(s[j].a[0]=='i')            {                printf("insert %d\n", s[j].x);              }              else              {                printf("getMin %d\n", s[j].x);              }          }      }      return 0; } 
原创粉丝点击