codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

681C.md (3897B)


      1 Source: https://codeforces.com/problemset/problem/681/C
      2 
      3 C. Heap Operations
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Petya has recently learned data structure named "Binary heap".
     22 
     23 The heap he is now operating with allows the following operations:
     24 
     25 * put the given number into the heap;
     26 * get the value of the minimum element in the heap;
     27 * extract the minimum element from the heap;
     28 
     29 Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
     30 
     31 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:
     32 
     33 * insert *x* — put the element with value *x* in the heap;
     34 * getMin *x* — the value of the minimum element contained in the heap was equal to *x*;
     35 * removeMin — the minimum element was extracted from the heap (only one instance, if there were many).
     36 
     37 All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
     38 
     39 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.
     40 
     41 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.
     42 
     43 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.
     44 
     45 Input
     46 
     47 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.
     48 
     49 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.
     50 
     51 Output
     52 
     53 The first line of the output should contain a single integer *m* — the minimum possible number of records in the modified sequence of operations.
     54 
     55 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.
     56 
     57 Note that the input sequence of operations must be the subsequence of the output sequence.
     58 
     59 It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
     60 
     61 Examples
     62 
     63 Input
     64 
     65 ```
     66 2  
     67 insert 3  
     68 getMin 4
     69 ```
     70 
     71 Output
     72 
     73 ```
     74 4  
     75 insert 3  
     76 removeMin  
     77 insert 4  
     78 getMin 4
     79 ```
     80 
     81 Input
     82 
     83 ```
     84 4  
     85 insert 1  
     86 insert 1  
     87 removeMin  
     88 getMin 2
     89 ```
     90 
     91 Output
     92 
     93 ```
     94 6  
     95 insert 1  
     96 insert 1  
     97 removeMin  
     98 removeMin  
     99 insert 2  
    100 getMin 2
    101 ```
    102 
    103 Note
    104 
    105 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.
    106 
    107 In the second sample case number 1 is inserted two times, so should be similarly removed twice.