Codeforces Round #357 (Div. 2)C:Heap Operations

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

题意:
你的电脑上存储着干个指令(有序),其中指令总共有三种:
①:insert  x→往堆里加一个数x
②:removeMin→移除堆里最小的那个数 
③:getMin  x→表示此时堆里最小值为x
悲惨的是:有一天你的表弟来你家玩,因为调皮删除了你的一些指令,这样你的指令连在一起就有可能变得不合法,例如堆里没有数还执行 removeMin操作等等,而你的任务就是添加尽可能少的指令,使这些指令连在一起合法。

解题思路:
用优先队列模拟,对于以下三种操作:
1、insert  x:一定合法,直接插入,x进队列
2、removeMin:有两种情况,一是当前队列不为空就合法,直接弹出顶端元素;二是当前队列为空,先插入个数(随便),再弹出
3、getMin  x:这个操作情况较多,有以下几种:
①当前队列为空,先补一个插入操作(当然是插入x),之后再getMin  x
②当前队列顶端元素比x小,补一个弹出操作
③当前队列顶端元素比x大,补一个插入操作(当然还是插入x),之后再getMin  x
④当前队列顶端元素刚好是x,合法,直接getMin原封不动


#include<stdio.h>
#include<string.h>
#include<functional>
#include<queue>
using namespace std;
typedef struct
{
	char str[15];
	int ans;
}Word;
Word s[1133333];
priority_queue<int, vector<int>, greater<int> > q;
int main(void)
{
	int n, i, b, a, k;
	char str[15];
	scanf("%d", &n);
	k = 0;
	for(i=1;i<=n;i++)
	{
		scanf("%s", str);
		if(str[0]=='i')
		{
			scanf("%d", &a);
			s[++k].ans = a, strcpy(s[k].str, str);
			q.push(a);
		}
		else if(str[0]=='r')
		{
			if(q.empty()!=0)
			{
				s[++k].ans = 1, strcpy(s[k].str, "insert");
				q.push(1);
			}
			strcpy(s[++k].str, str);
			q.pop();
		}
		else
		{
			scanf("%d", &a);
			if(q.empty()!=0)
			{
				q.push(a);
				s[++k].ans = a, strcpy(s[k].str, "insert");
			}
			b = q.top();
			if(b>a)
			{
				q.push(a);
				s[++k].ans = a, strcpy(s[k].str, "insert");
				b = a;
			}
			while(b<a)
			{
				strcpy(s[++k].str, "removeMin");
				q.pop();
				if(q.empty()!=0)
				{
					q.push(a);
					s[++k].ans = a, strcpy(s[k].str, "insert");
					break;
				}
				b = q.top();
				if(b>a)
				{
					q.push(a);
					s[++k].ans = a, strcpy(s[k].str, "insert");
					b = a;
				}
			}
			s[++k].ans = a, strcpy(s[k].str, str);
		}
	}
	printf("%d\n", k);
	for(i=1;i<=k;i++)
	{
		if(s[i].str[0]=='r')
			printf("%s\n", s[i].str);
		else
			printf("%s %d\n", s[i].str, s[i].ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值