Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- template<typename Type>
- class Node {
- public:
- Node(): prev(nullptr) {}
- Node(Type _val):val(_val), prev(nullptr) {}
- Node(Type _val, Node<Type> *_prev):val(_val), prev(_prev) {}
- Type &get_val() {
- return val;
- }
- Node<Type> *get_prev() const {
- return prev;
- }
- friend std::ostream &operator<<(std::ostream &stream, const Node<Type> &node) {
- stream << node.val << " ";
- return stream;
- }
- private:
- Type val;
- Node<Type> *prev;
- };
- template<typename Type>
- class Stack {
- public:
- Stack(): top_node(nullptr), cnt(0) {}
- ~Stack() {
- destroy_nodes();
- }
- void destroy_nodes() {
- while (top_node != nullptr) {
- auto prev = top_node->get_prev();
- delete(top_node);
- top_node = prev;
- }
- }
- size_t size() const {
- return cnt;
- }
- bool empty() const {
- return (top_node == nullptr);
- }
- Type &top() const {
- return top_node->get_val();
- }
- Stack<Type> &push(Type val) {
- ++cnt;
- top_node = new Node<Type>(val, top_node);
- return *this;
- }
- Stack<Type> &pop() {
- if (top_node != nullptr) {
- --cnt;
- auto prev = top_node->get_prev();
- delete(top_node);
- top_node = prev;
- }
- return *this;
- }
- friend std::ostream &operator<<(std::ostream &stream, const Stack<Type> &my_stack) {
- Node<Type> *node = my_stack.top_node;
- stream << "< ";
- while (node != nullptr) {
- stream << *node << " ";
- node = node->get_prev();
- }
- stream << ">";
- return stream;
- }
- private:
- Node<Type> *top_node;
- size_t cnt;
- };
- template<typename Type>
- class MinStack {
- public:
- size_t size() const {
- return data.size();
- }
- bool empty() const {
- return data.empty();
- }
- Type &top() const {
- return data.top();
- }
- Type &min() const {
- return minimum.top();
- }
- MinStack &push(const Type &element) {
- data.push(element);
- if (size() > 1 && minimum.top() < element) {
- minimum.push(minimum.top());
- } else {
- minimum.push(element);
- }
- return *this;
- }
- MinStack &pop() {
- data.pop();
- minimum.pop();
- return *this;
- }
- friend std::ostream &operator<<(std::ostream &stream, const MinStack &min_stack) {
- if (min_stack.empty()) {
- stream << "<empty>";
- } else {
- stream << "<top = " << min_stack.top() << ", minimum = " << min_stack.min() << ">";
- }
- return stream;
- }
- private:
- Stack<Type> data, minimum;
- };
- template<typename Type>
- class MinQueue {
- public:
- size_t size() const {
- return head.size() + tail.size();
- }
- bool empty() const {
- return (head.empty() && tail.empty());
- }
- void shift_if() {
- if (head.empty()) {
- while (!tail.empty()) {
- head.push(tail.top());
- tail.pop();
- }
- }
- }
- MinQueue &push(const Type &element) {
- tail.push(element);
- return *this;
- }
- MinQueue &pop() {
- shift_if();
- if (!head.empty()) {
- head.pop();
- }
- return *this;
- }
- Type &top() {
- shift_if();
- return head.top();
- }
- Type &min() {
- shift_if();
- if (tail.empty() || head.min() < tail.min()) {
- return head.min();
- }
- return tail.min();
- }
- private:
- MinStack<Type> head, tail;
- };
- int main() {
- int n, k, num;
- MinQueue<int> min_queue;
- std::cin >> n >> k;
- for (int i = 0; i < k; ++i) {
- std::cin >> num;
- min_queue.push(num);
- }
- std::cout << min_queue.min() << std::endl;
- for (int i = 0; i < n - k; ++i) {
- std::cin >> num;
- min_queue.pop();
- min_queue.push(num);
- std::cout << min_queue.min() << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement