您的位置:首页 > 运维架构

codeforces 681C Heap Operations

2016-08-21 22:07 309 查看
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.

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.

出题人上传的代码如下,很简练,赞

AC代码:

#include <iostream>
#include <queue>
#include <string>
using namespace std;
const int NO_X = 2e9;
const string REMOVE = "removeMin";
const string GET = "getMin";
const string INSERT = "insert";
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<pair<string, int> > ans;
priority_queue<int> q;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
if (s == INSERT) {
int x;
cin >> x;
q.push(-x);
ans.push_back(make_pair(s, x));
} else if (s == GET) {
int x;
cin >> x;
while (!q.empty() && -q.top() < x) {
q.pop();
ans.push_back(make_pair(REMOVE, NO_X));
}
if (q.empty() || -q.top() > x) {
q.push(-x);
ans.push_back(make_pair(INSERT, x));
}
ans.push_back(make_pair(s, x));
} else { // s == REMOVE
if (q.empty()) {
ans.push_back(make_pair(INSERT, 0));
} else {
q.pop();
}
ans.push_back(make_pair(s, NO_X));
}
}
cout << ans.size() << "\n";
for (int i=0; i<=ans.size()-1; i++) {
cout << ans[i].first;
if (ans[i].second != NO_X) {
cout << " " << ans[i].second;
}
cout << "\n";
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: