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 ofgetMin 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 removeMinare 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
题解:
  本题是真的debug了很久,其实屡清楚思路其实并不难。
  首先本题用到了优先队列,这个是解本题的基础
  什么是优先队列呢?
   调用头文件:
  #include<queue>
  using namespace std;  (queue模板需要定义容器类型)
       详细用法(部分): 
  priority_queue<Type> k;      ------      定义一个有序队列(其顶端元素为最大的那个)
  priority_queue<(队列中元素的数据类型), (用于储存和访问队列元素的底层容器的类型), (比较规则) > k      ------     (标准式)定义一个有序队列
  例如: priority_queue<int, vector<int>, greater<int> > k;(注意:比较规则后面必须要空上一格)
定义一个有序队列,排序规则为从大到小(其顶端元素为最小的那个)(greater顶端最小,less顶端最大)


  k.empty()      ------      查看是否为空范例,是的话返回1,不是返回0
  k.push(i)      ------      从已有元素后面增加元素i(队伍大小不预设)
  k.pop()      ------      清除位于顶端的元素(当然是排完序后,下同)
  k.top()      ------      显示顶端的元素
  k.size()      ------      输出现有元素的个数

然后再来看着道题的具体题解:
  首先有insert,getmin,removemin,三种方式。
insert和removemin这里不再细讲;
重点讨论一下getmin这种情况,当时真的是debug了太久。
(1)当队列为空,直接insert+getmin;
(2)当队列的当前最小值小于getmin里的最小值,这时需要不断移除队列里的最小值,直到大于或者等于
getmin里的最小值
(3)当队列里的当前最小值大于getmin时,直接insert;
(4)当队列里的最小值移除后,队列为空,直接insert;
   
  具体代码如下:
#include<functional>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
typedef struct
{
	char stu[20];
	int x;
}carry;
carry str[1000005],str1[1000005];
int  main()
{
	int n,m,j=0;
	priority_queue<int, vector<int>,greater<int> >k;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf(" %s",str[i].stu);
		if(strcmp(str[i].stu,"removeMin")!=0)
			scanf("%d",&str[i].x);
		if(strcmp(str[i].stu,"insert")==0)
		{
			k.push(str[i].x);
			str1[j++]=str[i];
		}
		else if(strcmp(str[i].stu,"removeMin")==0)
		{
			if(k.empty())
			{
				k.push(1);
				strcpy(str1[j].stu,"insert");
				str1[j++].x=1;
			}
			k.pop();
			str1[j++]=str[i];
		}
		else if(strcmp(str[i].stu,"getMin")==0)
		{
            if(k.empty())
			{
				strcpy(str1[j].stu,"insert");
				str1[j++].x=str[i].x;
				k.push(str[i].x);
				strcpy(str1[j].stu,"getMin");
				str1[j++].x=str[i].x;
			}
			else
			{
				while(k.top()<str[i].x)
				{
					strcpy(str1[j++].stu,"removeMin");
					k.pop();
					if(k.empty())
					{
						strcpy(str1[j].stu,"insert");
						str1[j++].x=str[i].x;
						k.push(str[i].x);
					}
				}
				if(k.top()>str[i].x)
				{
					strcpy(str1[j].stu,"insert");
					str1[j++].x=str[i].x;
					k.push(str[i].x);
				}
				if(k.top()==str[i].x)
				{
					strcpy(str1[j].stu,"getMin");
					str1[j++].x=str[i].x;
				}
			}
		}
	}
	printf("%d\n",j);
	for(int i=0;i<j;i++)
	{
		if(strcmp(str1[i].stu,"removeMin")==0)
			printf("%s\n",str1[i].stu);
		else
			printf("%s %d\n",str1[i].stu,str1[i].x);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值