Codeforces Round #357 (Div. 2)C. Heap Operations
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.
2
insert 3
getMin 4
4
insert 3
removeMin
insert 4
getMin 4
4
insert 1
insert 1
removeMin
getMin 2
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
优先队列模拟。
/* *********************************************** Author :guanjun Created Time :2016/8/9 9:47:21 File Name :cf357c.cpp ************************************************ */ #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <iomanip> #include <list> #include <deque> #include <stack> #define ull unsigned long long #define ll long long #define mod 90001 #define INF 1<<30 #define maxn 100010 #define cle(a) memset(a,0,sizeof(a)) const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; priority_queue<int,vector<int>,greater<int> >pq; string s; struct node{ string s; int x; }; vector<node>v; int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); int n,x; while(cin>>n){ v.clear(); while(!pq.empty())pq.pop(); for(int i=1;i<=n;i++){ cin>>s; if(s[0]=='i'){ scanf("%d",&x); pq.push(x); v.push_back({s,x}); } else if(s[0]=='g'){ scanf("%d",&x); if(pq.empty()){ v.push_back({"insert",x}); pq.push(x); } else while(!pq.empty()){ int y=pq.top(); if(y==x)break; else if(y>x){ v.push_back({"insert",x}); pq.push(x); break; } else{ pq.pop(); v.push_back({"removeMin",INF}); if(pq.empty()){ pq.push(x); v.push_back({"insert",x}); } } } v.push_back({s,x}); } else if(s[0]=='r'){ if(pq.empty())v.push_back({"insert",0}); else pq.pop(); v.push_back({"removeMin",INF}); } } cout<<v.size()<<endl; for(int i=0;i<v.size();i++){ cout<<v[i].s; if(v[i].x!=INF){ cout<<" "<<v[i].x; } cout<<endl; } } return 0; }
欢迎关注公众号: