Test instructions: Given n sequential commands, but possible commands are incomplete, allowing you to complete all the commands and ask for the fewest totals.
Analysis: Nothing to say, directly with the priority queue simulation on the line, insert, directly into the line, removemin, you have to judge the queue is not empty, and then consider Getmin, this is not the corresponding value, if the first element in the queue than it, then add one,
If the equality is taken directly, the smallest element in the queue is continuously fetched if it is less than.
The code is as follows:
#include <bits/stdc++.h>using namespace Std;char s[15], t[30];vector<string> ans;int main () {int n, x; while (CIN >> N) {ans.clear (); Priority_queue<int, Vector<int>, greater<int> > Q; for (int i = 0; i < n; ++i) {scanf ("%s", s); if (s[0] = = ' I ') {scanf ("%d", &x); sprintf (t, "Insert%d", x); Ans.push_back (String (t)); Q.push (x); } else if (s[0] = = ' R ') {if (Q.empty ()) {Ans.push_back ("Insert 1"); Q.push (1); } ans.push_back ("Removemin"); Q.pop (); } else{scanf ("%d", &x); while (true) {if (Q.empty () | | | q.top () > x) {q.push (x); sprintf (t, "Insert%d", x); Ans.push_back (String (t)); } else if (q.top () = = x) {break;} else{ans.push_back ("Removemin"); Q.pop (); }} sprintf (T, "Getmin%d", x); Ans.push_back (String (t)); }} printf ("%d\n", Ans.size ()); for (int i = 0; i < ans.size (); ++i) cout << ans[i] << Endl; } return 0;}
Codeforces 681C Heap Operations (simulation problem, priority queue)