Codeforces Round #357 (Div. 2) C. Heap Operations 模拟
C. Heap Operations
题目连接:
http://www.codeforces.com/contest/681/problem/C
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.
Sample Input
2
insert 3
getMin 4
Sample Output
4
insert 3
removeMin
insert 4
getMin 4
Hint
题意
他有一个小根堆,有三种操作
1.插入一个数
2.移除最小的数
3.输出最小的数
然后他做做做,记录了其中他操作的n次
这n次可能是不满足他的操作说明的。
现在让你完善它的操作,使他满足要求。
题解:
开个优先队列,然后正着模拟一遍就好了
插入一个数没什么好说的,照着做就好了
移除最小的数的话,如果队列为空,那么得先随便插一个才行
输出最小的数,如果输出的数比队首大,那么队列得pop到队首大于等于这个数才行。如果队首不是这个数,得push进去一个数才行。
代码
#include<bits/stdc++.h>
using namespace std;
priority_queue < int , vector < int > , greater < int > > Q;
vector<pair<char,int> >ans;
char s[444];
int main()
{
int n;scanf("%d",&n);
for(int i=0;i<n;i++)
{
int p;
scanf("%s",s);
if(s[0]!='r')cin>>p;
if(s[0]=='i')
{
Q.push(p);
ans.push_back(make_pair(s[0],p));
}
else if(s[0]=='r')
{
if(Q.empty())
{
Q.push(1);
ans.push_back(make_pair('i',1));
}
Q.pop();
ans.push_back(make_pair('r',0));
}
else if(s[0]=='g')
{
while(!Q.empty())
{
int t = Q.top();
if(t<p)
{
Q.pop();
ans.push_back(make_pair('r',0));
}
else break;
}
if(Q.empty()||Q.top()>p)
{
Q.push(p);
ans.push_back(make_pair('i',p));
}
ans.push_back(make_pair('g',p));
}
}
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++)
{
if(ans[i].first=='r')printf("removeMin ");
if(ans[i].first=='i')printf("insert ");
if(ans[i].first=='g')printf("getMin ");
if(ans[i].first!='r')printf("%d",ans[i].second);
printf("\n");
}
}
Codeforces Round #357 (Div. 2) C. Heap Operations 模拟的更多相关文章
- Codeforces Round #357 (Div. 2)C. Heap Operations
用单调队列(从小到大),模拟一下就好了,主要是getMin比较麻烦,算了,都是模拟....也没什么好说的.. #include<cstdio> #include<map> #i ...
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- Codeforces Round #357 (Div. 2) 优先队列+模拟
C. Heap Operations time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #357 (Div. 2) C
C. Heap Operations time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #284 (Div. 2)A B C 模拟 数学
A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序
A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #357 (Div. 2) E. Runaway to a Shadow 计算几何
E. Runaway to a Shadow 题目连接: http://www.codeforces.com/contest/681/problem/E Description Dima is liv ...
- Codeforces Round #357 (Div. 2) D. Gifts by the List 水题
D. Gifts by the List 题目连接: http://www.codeforces.com/contest/681/problem/D Description Sasha lives i ...
- Codeforces Round #357 (Div. 2) B. Economy Game 水题
B. Economy Game 题目连接: http://www.codeforces.com/contest/681/problem/B Description Kolya is developin ...
随机推荐
- AndroidManifest.xml权限设置
访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permi ...
- Django-模板继承、包含和静态文件配置
一.模板继承 模板继承可以减少页面内容的重复定义,实现页面内容的重用 典型应用:网站的头部.尾部是一样的,这些内容可以定义在父模板中,子模板不需要重复定义 block标签:在父模板中预留区域,在子模板 ...
- Pytorch数据读取框架
训练一个模型需要有一个数据库,一个网络,一个优化函数.数据读取是训练的第一步,以下是pytorch数据输入框架. 1)实例化一个数据库 假设我们已经定义了一个FaceLandmarksDataset数 ...
- 004_MAC实用的小工具
一.XtraFinder(右键菜单扩展) http://www.xuebuyuan.com/173454.html http://www.mamicode.com/info-detail-111618 ...
- drop out为什么能够防止过拟合
来源知乎: dropout 的过程好像很奇怪,为什么说它可以解决过拟合呢?(正则化) 取平均的作用: 先回到正常的模型(没有dropout),我们用相同的训练数据去训练5个不同的神经网络,一般会得到5 ...
- 链家2018春招C/C++开发实习生在线考试编程题
题目一 题解:该题目意思就是让你输入n组数据,然后求并集,利用STL容器set集合的特性:元素不重复存储,我们可以很轻易得出答案 #include <iostream> #include ...
- SQL农历转换函数(显示中文格式,加入润月的显示)
if object_id('fn_getlunar') is not null drop function fn_getlunar go create function dbo.fn_getlunar ...
- hdu 4664 划线(SG)
N个平面,每个平面有ni个点 两个人玩游戏,划线,他们可以划任意一个平面的两个点,有以下要求:两个人划得线不能交叉,不要划已经划过的线,如果一个平面被划了一个空心的三角形,那么这个平面就不能继续划线了 ...
- day1作业:编写登录窗口一个文件实现
思路: 1.参考模型,这个作业我参考了linux的登录认证流程以及结合网上银行支付宝等锁定规则: 1)认证流程参考的是Linux的登录:当你输入完用户名密码后再验证用户名是否存在用户是否被锁定,然后在 ...
- bzoj 1237 [SCOI2008]配对 贪心+dp
思路:dp[ i ] 表示 排序后前 i 个元素匹配的最小值, 我们可以发现每个点和它匹配的点的距离不会超过2,这样就能转移啦. #include<bits/stdc++.h> #defi ...