Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- class MinQueue {
- private:
- vector<int> data;
- size_t dataBegin = 0;
- vector<int> minData;
- size_t minDataBegin = 0;
- public:
- void push(int val) {
- data.push_back(val);
- while (minData.size() > minDataBegin && minData.back() > val) {
- minData.pop_back();
- }
- minData.push_back(val);
- }
- int min() {
- return minData[minDataBegin];
- }
- void pop() {
- if (minData[minDataBegin] == data[dataBegin++]) {
- ++minDataBegin;
- }
- }
- };
- int main() {
- MinQueue q;
- int n;
- cin >> n;
- string type;
- int val;
- while (n--) {
- cin >> type;
- if (type == "push") {
- cin >> val;
- q.push(val);
- } else if (type == "pop") {
- q.pop();
- } else {
- cout << q.min() << '\n';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement