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

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

题目连接:http://codeforces.com/contest/681/problem/C


题意:一个最小值优先的优先队列。inisert x是在优先队列中插入x,removeMin是删除队首元素,getMin x是优先队列返回的最高级元素是x。输出m个操作,使得输入的n个操作能够成立。

思路:优先队列的板子题。

代码:

#include<bits/stdc++.h>
using namespace std;
char s[];
struct cmp
{
bool operator ()(int &a,int &b)
{
return a>b;
}
};
priority_queue<int,vector<int>,cmp>Q;
int sign[];
int num[];
int main()
{
int i,j,n,x;
cin>>n;
getchar();
for(i=,j=; i<n; i++)
{
scanf("%s",s);
if(s[]=='i')
{
scanf("%d",&x);
getchar();
sign[j]=;
num[j++]=x;
Q.push(x);
}
else if(s[]=='r')
{
if(!Q.empty()) Q.pop();
else
{
sign[j]=;
num[j++]=;
}
sign[j]=-;
num[j++]=;
}
else
{
scanf("%d",&x);
getchar();
while(!Q.empty()&&Q.top()<x)
{
sign[j]=-;
num[j++]=x;
Q.pop();
}
if(Q.empty()||Q.top()!=x)
{
sign[j]=;
num[j++]=x;
Q.push(x);
}
sign[j]=;
num[j++]=x;
}
}
cout<<j<<endl;
for(i=; i<j; i++)
{
if(sign[i]==) cout<<"getMin "<<num[i]<<endl;
else if(sign[i]==) cout<<"insert "<<num[i]<<endl;
else cout<<"removeMin"<<endl;
}
return ;
}

传送门:优先队列模板

 #include<iostream>
#include<functional>
#include<queue>
#include<vector>
#include<cstdio>
using namespace std; //定义比较结构
struct cmp1
{
bool operator ()(int &a,int &b)
{
return a>b;//最小值优先
}
}; struct cmp2
{
bool operator ()(int &a,int &b)
{
return a<b;//最大值优先
}
}; //自定义数据结构
struct number1
{
int x;
bool operator < (const number1 &a) const
{
return x>a.x;//最小值优先
}
};
struct number2
{
int x;
bool operator < (const number2 &a) const
{
return x<a.x;//最大值优先
}
};
int a[]= {,,,,,,,,,,,};
number1 num1[]= {,,,,,,,,,,,};
number2 num2[]= {,,,,,,,,,,,}; int main()
{
priority_queue<int>que;//采用默认优先级构造队列 priority_queue<int,vector<int>,cmp1>que1;//最小值优先
priority_queue<int,vector<int>,cmp2>que2;//最大值优先 priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误,
priority_queue<int,vector<int>,less<int> >que4;////最大值优先 priority_queue<number1>que5; //最小优先级队列
priority_queue<number2>que6; //最大优先级队列 int i;
for(i=; a[i]; i++)
{
que.push(a[i]);
que1.push(a[i]);
que2.push(a[i]);
que3.push(a[i]);
que4.push(a[i]);
}
for(i=; num1[i].x; i++)
que5.push(num1[i]);
for(i=; num2[i].x; i++)
que6.push(num2[i]); printf("采用默认优先关系:\n(priority_queue<int>que;)\n");
printf("Queue 0:\n");
while(!que.empty())
{
printf("%3d",que.top());
que.pop();
}
puts("");
puts(""); printf("采用结构体自定义优先级方式一:\n(priority_queue<int,vector<int>,cmp>que;)\n");
printf("Queue 1:\n");
while(!que1.empty())
{
printf("%3d",que1.top());
que1.pop();
}
puts("");
printf("Queue 2:\n");
while(!que2.empty())
{
printf("%3d",que2.top());
que2.pop();
}
puts("");
puts("");
printf("采用头文件\"functional\"内定义优先级:\n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)\n");
printf("Queue 3:\n");
while(!que3.empty())
{
printf("%3d",que3.top());
que3.pop();
}
puts("");
printf("Queue 4:\n");
while(!que4.empty())
{
printf("%3d",que4.top());
que4.pop();
}
puts("");
puts("");
printf("采用结构体自定义优先级方式二:\n(priority_queue<number>que)\n");
printf("Queue 5:\n");
while(!que5.empty())
{
printf("%3d",que5.top());
que5.pop();
}
puts("");
printf("Queue 6:\n");
while(!que6.empty())
{
printf("%3d",que6.top());
que6.pop();
}
puts("");
return ;
}
/*
运行结果 :
采用默认优先关系:
(priority_queue<int>que;)
Queue 0:
83 72 56 47 36 22 14 10 7 3 采用结构体自定义优先级方式一:
(priority_queue<int,vector<int>,cmp>que;)
Queue 1:
7 10 14 22 36 47 56 72 83 91
Queue 2:
83 72 56 47 36 22 14 10 7 3 采用头文件"functional"内定义优先级:
(priority_queue<int,vector<int>,greater<int>/less<int> >que;)
Queue 3:
7 10 14 22 36 47 56 72 83 91
Queue 4:
83 72 56 47 36 22 14 10 7 3 采用结构体自定义优先级方式二:
(priority_queue<number>que)
Queue 5:
7 10 14 22 36 47 56 72 83 91
Queue 6:
83 72 56 47 36 22 14 10 7 3
*/

各种优先队列姿势


Codeforces 681C. Heap Operations 优先队列的更多相关文章

  1. CodeForces 681C Heap Operations (模拟题,优先队列)

    题意:给定 n 个按顺序的命令,但是可能有的命令不全,让你补全所有的命令,并且要求让总数最少. 析:没什么好说的,直接用优先队列模拟就行,insert,直接放入就行了,removeMin,就得判断一下 ...

  2. CodeForces 681C Heap Operations(模拟)

    比较简单的模拟,建议使用STL优先队列. 代码如下: #include<iostream> #include<cstdio> #include<cstring> # ...

  3. Heap Operations 优先队列

    Petya has recently learned data structure named "Binary heap". The heap he is now operatin ...

  4. Codeforces Round #357 (Div. 2) C. Heap Operations 模拟

    C. Heap Operations 题目连接: http://www.codeforces.com/contest/681/problem/C Description Petya has recen ...

  5. Heap Operations(模拟题)

     Heap Operations time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

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

    用单调队列(从小到大),模拟一下就好了,主要是getMin比较麻烦,算了,都是模拟....也没什么好说的.. #include<cstdio> #include<map> #i ...

  7. Codeforces 948C Producing Snow(优先队列+思维)

    题目链接:http://codeforces.com/contest/948/problem/C 题目大意:给定长度n(n<=1e5),第一行v[i]表示表示第i堆雪的体积,第二行t[i]表示第 ...

  8. Codeforces Gym 101291C【优先队列】

    <题目链接> 题目大意: 就是一道纯模拟题,具体模拟过程见代码. 解题分析:要掌握不同优先级的优先队列的设置.下面是对优先队列的使用操作详解: priority_queue<int& ...

  9. CodeForces - 799B-T-shirt buying (优先队列)

    题目链接 /* Name: Copyright: Author: Date: 2018/5/2 16:09:54 Description:优先队列 */ #include <iostream&g ...

随机推荐

  1. 数据库入门4 结构化查询语言SQL

    知识内容: 1.了解SQL 2.库.表操作及索引 3.select语句及insert语句 4.update语句与delete语句 5.SQL常用函数 6.多表连接及组合查询 7.视图操作及数据控制 参 ...

  2. https://127.0.0.1:8080/test?param={%22..报错

    使用场景:spring boot 1.5.x,内置的tomcat版本为8.5.1 原因: tomcat自tomcat 8.0.35版本之后对URL参数做了比较规范的限制,必须遵循RFC 7230 an ...

  3. Coxph model Pvalue Select2

    4   1) Put summary(coxphobject) into a variable summcph <- summary(coxphobject) 2) examine it wit ...

  4. Javascript,获取元素,write方法

    一:Javascript:弱类型脚本语言,是一种动态类型.实现部分动画效果和用户交互等 -- html是骨架(页面结构)  css样式  js是行为 -- 弱类型体现: JS代码可以写在body,he ...

  5. placeholder测试

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  6. leetcode150

    public class Solution { public int EvalRPN(string[] tokens) { Stack<int> ST_NUM = new Stack< ...

  7. leetcode453

    public class Solution { public int MinMoves(int[] nums) { var list = nums.OrderBy(x => x).ToList( ...

  8. maven错误

    maven-enforcer-plugin (goal "enforce") is ignored by m2e. Plugin execution not covered by ...

  9. Ambari安装Hadoop集群

    * System Environment:centOS6.7 1.Prepare the Environment 1)Set Up Password-less SSH : (Generate publ ...

  10. python练习题:三级菜单

    需求:可依次选择进入各子菜单可从任意一层往回退到上一层可从任意一层退出程序所需新知识点:列表.字典 测试环境:win7系统,python3.7.0,工具:pycharm-community-2018. ...