• Codeforces Round #357 (Div. 2) C


    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 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.

    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 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.

    题意:对一个堆有三种操作

           1.insert x 插入一个数 x

           2.getMin x 获得最小值x

           3.removeMin 删除最小值 

       给你n个一系列的操作(不是完整的操作) 要求你补全操作(使得操作步数尽量小)

    题解:优先队列存储  模拟过程

           注意

    input:

    1

    removeMin

    output:

    insert 0

    removeMin

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 #include<queue>
     6 using namespace std;
     7 priority_queue<int, vector<int>, greater<int> > q;
     8 int n;
     9 char s[50];
    10 int w;
    11 int a[1000005],b[1000005];
    12 int main()
    13 {
    14     scanf("%d",&n);
    15     int sum=1;
    16     memset(a,0,sizeof(a));
    17     memset(b,0,sizeof(b));
    18     while(!q.empty())
    19       q.pop();
    20     for(int i=1;i<=n;i++)
    21     { 
    22         scanf("%s",s);
    23         if(s[0]=='i')
    24         {
    25             scanf("%d",&w);
    26             a[sum]=1;
    27             b[sum++]=w;
    28             q.push(w);
    29         } 
    30         if(s[0]=='g')
    31         {    
    32             scanf("%d",&w);
    33             if(q.empty()||w<q.top())
    34             {
    35             a[sum]=1;
    36             b[sum++]=w;
    37             q.push(w);
    38             a[sum]=2;
    39             b[sum++]=w;
    40             }
    41             else
    42             {
    43                     while(!q.empty()&&w>q.top())
    44                         {
    45                          a[sum]=3;
    46                          b[sum++]=0;
    47                          q.pop();
    48                         }
    49                       if(q.empty()||w<q.top())
    50                       {
    51                         a[sum]=1;
    52                         b[sum++]=w;
    53                         q.push(w);
    54                         a[sum]=2;
    55                         b[sum++]=w;
    56                     }
    57                      else
    58                     {
    59                          a[sum]=2;
    60                          b[sum++]=w;
    61                     }
    62             }
    63         }
    64         if(s[0]=='r')
    65         {
    66             while(q.empty())
    67             {
    68                 a[sum]=1;
    69                 b[sum++]=0;
    70                 q.push(0);
    71                 
    72             }   
    73                 a[sum]=3;
    74                 b[sum++]=0;
    75                 q.pop();
    76         } 
    77     }
    78      printf("%d
    ",sum-1);
    79      for(int i=1;i<sum;i++)
    80      {
    81          if(a[i]==1)
    82          printf("insert %d
    ",b[i]);
    83          if(a[i]==2)
    84          printf("getMin %d
    ",b[i]);
    85          if(a[i]==3)
    86          printf("removeMin
    ");
    87      }
    88     return 0;
    89 }
  • 相关阅读:
    鼠标经过显示边框
    特殊字符
    HTML 列表
    embed 引入网上视频
    锚点定位
    盒子阴影
    Map的四种遍历方式
    Glide的 java.lang.RuntimeException: Expected instanceof GlideModule, but found:X.GlideModule@2e4554f
    Java标识符的命名规则
    django入门与实践
  • 原文地址:https://www.cnblogs.com/hsd-/p/5612515.html
Copyright © 2020-2023  润新知