Codeforces Round #357 (Div. 2)-C. Heap Operations
来源:互联网 发布:网络管理协议snmp组成 编辑:程序博客网 时间:2024/06/05 04:52
原题链接
模拟题,用优先队列存数.当遇到removeMin时,删除优先队列中第一个数,若优先队列为空,先insert任意个数再删除。遇到getMin a,对优先队列removeMin,直到遇到一个比大于等于a的数,或队列为空时停止,若队列为空或队首元素不为a,则insert a。
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <stack>#include <queue> #include <cmath>#include <algorithm>#define maxn 1005 #define INF 1e9typedef long long ll;using namespace std;char ans[1000005][30];char s[30];int num[1000005];int main(){//freopen("in.txt", "r", stdin);int n, cnt = -1, a;priority_queue<int, vector<int>, greater<int> >q; scanf("%d", &n);for(int i = 0; i < n; i++){scanf("%s", s);if(s[0] == 'i'){scanf("%d", &a);q.push(a);strcpy(ans[++cnt], s);num[cnt] = a;}else if(s[0] == 'g'){scanf("%d", &a);while(!q.empty() && q.top() < a){ int d = q.top();num[++cnt] = d;strcpy(ans[cnt], "removeMin");q.pop();}if(q.empty() || q.top() != a){++cnt;q.push(a);num[cnt] = a;strcpy(ans[cnt], "insert");}++cnt;num[cnt] = a;strcpy(ans[cnt], s);}else{if(q.empty()){num[++cnt] = 0;strcpy(ans[cnt], "insert");}else{q.pop();}++cnt;strcpy(ans[cnt], "removeMin");}}printf("%d\n", cnt+1); for(int i = 0; i <= cnt; i++){printf("%s", ans[i]);if(ans[i][0] != 'r'){printf(" %d", num[i]);}puts("");}return 0;}
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.
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.
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.
2insert 3getMin 4
4insert 3removeMininsert 4getMin 4
4insert 1insert 1removeMingetMin 2
6insert 1insert 1removeMinremoveMininsert 2getMin 2
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 4one 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.
- Codeforces Round #357 (Div. 2) C. Heap Operations
- Codeforces Round #357 (Div. 2)C. Heap Operations
- Codeforces Round #357 (Div. 2)-C. Heap Operations
- Codeforces Round #357 (Div. 2) C. Heap Operations
- Codeforces Round #357 (Div. 2)-C. Heap Operations(优先队列)
- Codeforces Round #357 (Div. 2)C:Heap Operations
- Codeforces Round #357 (Div. 2) C Heap Operations(模拟)
- Codeforces Round #357 (Div. 2) C Heap Operations (模拟)
- Codeforces-Round-#357-(Div.-2)-Heap-Operations
- Codeforces Round #357 (Div. 2) C. Heap Operations (优先队列)
- Codeforces Round #357 (Div. 2) -- C. Heap Operations (优先队列模拟)
- Codeforces Round #357 (Div. 2) C. Heap Operations(优先队列)
- Codeforces Round #357 (Div. 2) C. Heap Operations(模拟,优先队列)
- CodeForces 681C Heap Operations
- codeforces 681C Heap Operations
- CodeForces 681C Heap Operations
- CodeForces 681C Heap Operations
- Codeforces Round #307 (Div. 2)--C. GukiZ hates Boxes、D. GukiZ and Binary Operations
- Swift学习之九:基本运行符(Basic Operators)
- Android常用开源工具(2)-Dagger2进阶
- 网红“有毒”,被“感染”的短视频、直播平台真能赢得下半场?
- Swift学习之十:字符串与字符(Strings and Characters)
- Java基础知识:java.util.Map集合
- Codeforces Round #357 (Div. 2)-C. Heap Operations
- byte数组和图片转换
- linux基础之压缩与归档
- /etc/fstab发生错误或磁盘错误导致系统无法启动的修复方法
- Android常用开源工具(1)-Dagger2入门
- 使用boost::filesystem实现目录遍历
- jquery无法控制checkbox是否选中
- sybase安装过程Accessing a corrupted shared library
- Swift学习之十一:数组与字典(Array and Dictionary are collection types)