CF681C.Heap Operations
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
佩佳最近学习了一种名为“二叉堆”的数据结构。
他当前操作的堆支持以下三种操作:
- 将给定数字插入堆中;
- 获取堆中最小元素的值;
- 从堆中删除最小元素;
因此,在任意时刻,堆中都包含若干个整数(可能为空),其中某些数可能相等。
为了更好地掌握该数据结构,佩佳从一个空堆开始,对其执行了若干上述操作。同时,他仔细地将所有操作及其结果记录在事件日志中,格式如下:
insert x— 将值为x的元素插入堆中;getMin x— 堆中最小元素的值等于x;removeMin— 从堆中删除最小元素(若存在多个相同最小值,仅删除其中一个)。
所有操作均合法,即每次执行 getMin 或 removeMin 操作时,堆中至少有一个元素。
佩佳外出吃午饭期间,他的弟弟沃瓦来到房间,拿走了佩佳日志中的若干页,并用它们折成了纸船。
现在沃瓦担心自己使佩佳的操作序列变得不一致。例如,若严格按照日志中记录的操作顺序逐一执行,则 getMin 操作的实际结果可能与日志中所记录的不符;此外,部分 getMin 或 removeMin 操作可能不合法(例如在堆为空时执行)。
现在沃瓦希望向当前日志中添加若干新的操作记录,使得最终的操作序列是合法的:即每个 getMin 操作的实际结果与其日志中记录的值完全一致,且每次执行 getMin 或 removeMin 操作时堆均非空。沃瓦希望尽快完成这项工作,因为佩佳随时可能回来。他请你帮忙,求出需要添加的最少操作记录数。注意:可在日志开头、任意两个已有操作之间或日志末尾添加任意数量的操作。
输入格式
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.
输入的第一行包含唯一一个整数 n(1 ≤ n ≤ 100000)——表示彼得的日志中剩余的记录条数。
接下来的 n 行按当前日志中记录被应用的顺序描述这些记录。记录格式如题面所述。输入中的所有数字均为整数,且其绝对值不超过 109。
输出格式
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.
输出的第一行应包含一个整数 m —— 修改后的操作序列中可能的最少记录数。
接下来的 m 行应按应用顺序(即输入格式所描述的格式,见题目陈述)输出修正后的记录序列,每行一条。输出中的所有数字均为整数,其绝对值不超过 109。
注意:输入的操作序列必须是输出序列的一个子序列。
保证存在一个正确答案,其所含操作数不超过 1000000。
输入输出样例
输入#1
2 insert 3 getMin 4
输出#1
4 insert 3 removeMin insert 4 getMin 4
输入#2
4 insert 1 insert 1 removeMin getMin 2
输出#2
6 insert 1 insert 1 removeMin removeMin insert 2 getMin 2
说明/提示
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.
在第一个样例中,将数字 3 插入堆后,堆中的最小值为 3。为了使第一次 getMin 的结果等于 4,应首先从堆中删除数字 3,然后将数字 4 插入堆中。
在第二个样例中,数字 1 被插入了两次,因此也应相应地删除两次。
输入解题思路,AI测评打分。不知道怎么写?