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

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 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 each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMinare 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 4one 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.

题意:弟弟在哥哥吃饭的时候把他的一些写有步骤的纸给那去折纸船了,现在要你用尽可能少的补充步骤,使得每次getMin 时不会矛盾

题目链接:Heap-Operations

解题思路:用上升优先队列记住每次getMin时的最小值,若冲突的话,相应的增加insert 或者 removeMin 操作即可,

另外要注意,当队列为空时,removeMin 之前必定要先insert,大致直接模拟。

代码:

// Heap Operations
//题目链接:http://www.codeforces.com/problemset/problem/681/C
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
        
        
#include
        
        
          using namespace std; struct node { string s; int num; }b[1000005]; int main() { int n,i,num; string s; while(~scanf("%d",&n)){ int cnt=0; priority_queue 
         
           , greater 
          
            > qe; //队列按从小到大排列 for(i=0;i 
           
             >s; if(s[0] != 'r') //removeMin 时,没有数字输入 scanf("%d",&num); if(s[0] == 'i') { qe.push(num); //insert } else if(s[0] == 'r') { if(qe.empty()) { //空队列时,不能removeMin,那么少了insert b[cnt].s="insert"; b[cnt++].num=0; qe.push(0); } qe.pop(); //removeMin } else if(s[0] == 'g') { while(!qe.empty() && qe.top() < num) { //当队头元素小于所要取得最小值,那么之前一定把这个队头给pop了 b[cnt++].s="removeMin"; qe.pop(); } if(qe.empty() || qe.top() > num) { //队头为空时,或者队头元素大于要去的最小值时,代表没有这个数字,就要insert b[cnt].s="insert"; b[cnt++].num=num; qe.push(num); } } b[cnt++].s=s; //补充完可能失去的操作,才按顺序把正常的操作存储 if(s[0] != 'r') b[cnt-1].num=num; } printf("%d\n",cnt); for(i=0;i 
             
            
           
          
        
       
       
      
      
     
     
    
    

补充:自己在优先队列的排列顺序上搞错了,结果WA了不少次。。。。。。经验太少了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值