Codeforces 681C Heap Operations (simulation problem, priority queue)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.