zoukankan      html  css  js  c++  java
  • Heap Operations(模拟题)

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

     题意:模拟一个最小堆操作,给出一组操作,操作可能是不合法的,让你补充操作让它合法。这是道模拟题,用个优先队列模拟一下就行了,注意改下优先队列的优先级。本题要注意堆空的时候也会有removeMIn的操作,以及在一开始getmin遇到个大的数,pop后的数可能就是你的num了,这时候就不用insert了。还有一个神坑,就是数组要开大点!!他的命令最多九个字符,还要算上n,而n最大有9位数,还有可能是负的,所以开个25比较合适。(PS:博主就是被这个坑在第十组数据)

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    using namespace std;
    typedef long long ll;
    char ans[1000010][35];
    int main()
    {
        priority_queue <int,vector<int> ,greater<int> > Q;
        char ch[10];
        int num;
        int n,pos=0;
        scanf("%d",&n);
        for (int i=0; i<n; i++)
        {
            scanf("%s%d",ch,&num);
            if (strcmp(ch,"insert")==0)
            {
                Q.push(num);
                sprintf(ans[pos++],"%s %d","insert",num);
            }
            else if (strcmp(ch,"getMin")==0)
            {
                if (Q.empty())
                {
                    sprintf(ans[pos++],"%s %d","insert",num);
                    Q.push(num);
                }
                else if (num!=Q.top())
                {
                    while (!Q.empty()&&num>Q.top())
                    {
                        sprintf(ans[pos++],"%s","removeMin");
                        Q.pop();
                    }
                    if (Q.empty()||Q.top()!=num)
                    {
                        Q.push(num);
                        sprintf(ans[pos++],"%s %d","insert",num);
                    }
                }
                sprintf(ans[pos++],"%s %d","getMin",num);
            }
            else
            {
                if (!Q.empty())
                {
                    Q.pop();
                    sprintf(ans[pos++],"%s","removeMin");
                }
                else
                {
                    sprintf(ans[pos++],"%s %d","insert",0);//这里插入什么数字都可以
                    sprintf(ans[pos++],"%s","removeMin");
                }
            }
        }
        printf("%d
    ",pos);
        for (int i=0; i<pos; i++)
            printf("%s
    ",ans[i]);
        return 0;
    }
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • 相关阅读:
    单例设计模式
    程序员眼中的中国传统文化王阳明《传习录》10
    程序员眼中的中国传统文化王阳明《传习录》8
    程序员眼中的中国传统文化王阳明《传习录》16
    程序员眼中的中国传统文化王阳明《传习录》11
    程序员眼中的中国传统文化王阳明《传习录》15
    程序员眼中的中国传统文化王阳明《传习录》14
    程序员眼中的中国传统文化王阳明《传习录》9
    程序员眼中的中国传统文化王阳明《传习录》13
    程序员眼中的中国传统文化王阳明《传习录》12
  • 原文地址:https://www.cnblogs.com/scaugsh/p/5586633.html
Copyright © 2011-2022 走看看