Codeforces Round #357 (Div. 2) Heap Operations
2016-06-16 20:15
225 查看
Heap Operations
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.
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.
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.
insert 3
getMin 4
insert 3
removeMin
insert 4
getMin 4
题意
开个优先队列,然后正着模拟
插入一个数不用变
移除最小的数的话,如果队列为空,那么得先随便插一个才行
输出最小的数,如果输出的数比队首大,那么队列得pop到队首大于等于这个数才行。如果队首不是这个数,得push进去一个数才行。
一开始用cout居然超时了,改为printf()就400+ms过了。
代码
题目连接:
http://www.codeforces.com/contest/681/problem/CDescription
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
2insert 3
getMin 4
Sample Output
4insert 3
removeMin
insert 4
getMin 4
题意
开个优先队列,然后正着模拟
插入一个数不用变
移除最小的数的话,如果队列为空,那么得先随便插一个才行
输出最小的数,如果输出的数比队首大,那么队列得pop到队首大于等于这个数才行。如果队首不是这个数,得push进去一个数才行。
一开始用cout居然超时了,改为printf()就400+ms过了。
代码
#include<bits/stdc++.h> using namespace std; const int maxn = 1000000+100; const string s1 = "insert"; const string s2 = "getMin"; const string s3 = "removeMin"; priority_queue<int,vector < int > , greater < int > > q; struct node{ int k,v; }a[maxn]; int main() { int n; int ans=0; scanf("%d",&n); while(n--){ string s;int v; cin>>s; if(s==s3){ if(q.empty()){ ans++;a[ans].k=1;a[ans].v=1; } else q.pop(); ans++;a[ans].k=3; } else{ cin>>v; if(s==s1){q.push(v);ans++;a[ans].k=1;a[ans].v=v;} else{ while(!q.empty()){ if(q.top()<v){ q.pop();ans++;a[ans].k=3; } else break; } if(q.empty()||q.top()>v){ q.push(v);ans++;a[ans].k=1,a[ans].v=v; } ans++;a[ans].k=2,a[ans].v=v; } } } printf("%d\n",ans); for(int i=1;i<=ans;i++){ if(a[i].k==3) printf("removeMin\n"); if(a[i].k==2) printf("getMin %d\n",a[i].v); if(a[i].k==1) printf("insert %d\n",a[i].v); } return 0; }
相关文章推荐
- Linux上安装和配置Hadoop
- linux yum命令详解
- XenDesktop7.9的新功能:Citrix MCS IO加速浅析
- OpenCV 区域编码和阈值编码实现图像压缩(8*8DCT变换,保留50%的系数)
- 3、ubuntu 下编译 linux 内核时无法使用 make menuconfig
- how to open ubuntu port like 8443
- linux常用小技巧(持续更新中)
- centos session
- yarn资源管理器高可用性的实现
- Centos下Yum安装PHP5.5,5.6,7.0
- @property修饰符
- linux 中断机制的处理过程
- Linux下python matplotlib.pyplot在图像上显示中文的问题
- shell脚本操作mysql数据库
- Bash相关的环境变量配置
- zabbix监控activemq队列脚本
- Apache shiro集群实现 (八) web集群时session同步的3种方法
- Jenkins使用shell脚本部署到远程服务器步骤
- Apache shiro集群实现 (七)分布式集群系统下---cache共享
- nginx配置文件解析