CodeForces 681C 模拟-Heap Operations

来源:互联网 发布:jquery.base64.js中文 编辑:程序博客网 时间:2024/06/05 17:59

Description

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.

题意:


一个二叉堆,存在三种操作:
插入一个值
取最小值
去掉一个最小值(最小值可能有多个)
现在有一些操作和操作的结果,插入最少的操作使得操作的结果正确。


题解:


模拟类题目,模拟操作即可。
二叉堆的三个操作:
插入一个值:不需要处理直接插入
取最小值:分多种情况,如果此时二叉堆的最小值跟结果一样,不需要处理,如果比结果大,那么插入结果的值,如果比结果的值小,那么去掉这个值直到最小值等于结果的值,如果不存在结果的值,就插入一个。
去掉一个最小值:如果二叉堆不为空,不需要处理,如果为空,随便插入一个值。


#include <iostream>#include <queue>#include <vector>#include <functional>#include <string>#include <string.h>#include <cmath>#include <map>#include <stdio.h>#define MAXN 1000000 + 1using namespace std;struct cmp1{    bool operator ()(int &a,int &b){        return a>b;//最小值优先    }};priority_queue<int,vector<int>,cmp1>q;int re[2][MAXN];char s[25];int getnum(){    int k = 0;    int sum = 0;    for(int i = strlen(s) - 1; i >= 0 && s[i] != ' '; i--)    {        if(s[i] != '-')            sum += (s[i] - '0') * (int)ceil(pow(10,k++));        else            sum = -1 * sum;    }    return sum;}int main(){    int n;    int sum;    while(cin >> n)    {        sum = 0;        memset(re, 0, sizeof(re));        scanf("%*c");        while(!q.empty()) q.pop();        while(n--)        {            gets(s);            if(s[0] == 'i')            {                int t = getnum();                re[0][sum] = 1;                re[1][sum++] = t;                q.push(t);            }            else if(s[0] == 'r')            {                if(q.empty())                {                    re[0][sum] = 1;                    re[1][sum++] = 0;                    re[0][sum] = 2;                    re[1][sum++] = 0;                }                else                {                    re[0][sum] = 2;                    re[1][sum++] = 0;                    q.pop();                }            }            else if(s[0] == 'g')            {                int t = getnum();                if(q.empty())                {                    re[0][sum] = 1;                    re[1][sum++] = t;                    re[0][sum] = 3;                    re[1][sum++] = t;                    q.push(t);                }                else                {                    if(q.top() > t)                    {                        re[0][sum] = 1;                        re[1][sum++] = t;                        re[0][sum] = 3;                        re[1][sum++] = t;                        q.push(t);                    }                    else if(q.top() < t)                    {                        while(!q.empty() && q.top() < t)                        {                            re[0][sum] = 2;                            re[1][sum++] = 0;                            q.pop();                        }                        if(q.empty() || q.top() > t)                        {                            re[0][sum] = 1;                            re[1][sum++] = t;                            re[0][sum] = 3;                            re[1][sum++] = t;                            q.push(t);                        }                        else if(q.top() == t)                        {                            re[0][sum] = 3;                            re[1][sum++] = t;                        }                    }                    else if(q.top() == t)                    {                        re[0][sum] = 3;                        re[1][sum++] = t;                    }                }            }        }        printf("%d\n", sum);        for(int i = 0; i < sum; i++)        {            switch(re[0][i])            {            case 1:                printf("insert %d\n", re[1][i]);                break;            case 2:                printf("removeMin\n");                break;            case 3:                printf("getMin %d\n", re[1][i]);                break;            default:                break;            }        }    }    return 0;}
0 0
原创粉丝点击