Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template<typename Type>
  4. class Node {
  5.  public:
  6.     Node(): prev(nullptr) {}
  7.     Node(Type _val):val(_val), prev(nullptr) {}
  8.     Node(Type _val, Node<Type> *_prev):val(_val), prev(_prev) {}
  9.  
  10.     Type &get_val() {
  11.         return val;
  12.     }
  13.  
  14.     Node<Type> *get_prev() const {
  15.         return prev;
  16.     }
  17.  
  18.     friend std::ostream &operator<<(std::ostream &stream, const Node<Type> &node) {
  19.         stream << node.val << " ";
  20.         return stream;
  21.     }
  22.  
  23.  private:
  24.     Type val;
  25.     Node<Type> *prev;
  26. };
  27.  
  28. template<typename Type>
  29. class Stack {
  30.  public:
  31.     Stack(): top_node(nullptr), cnt(0) {}
  32.  
  33.     ~Stack() {
  34.         destroy_nodes();
  35.     }
  36.  
  37.     void destroy_nodes() {
  38.         while (top_node != nullptr) {
  39.             auto prev = top_node->get_prev();
  40.             delete(top_node);
  41.             top_node = prev;
  42.         }
  43.     }
  44.  
  45.     size_t size() const {
  46.         return cnt;
  47.     }
  48.  
  49.     bool empty() const {
  50.         return (top_node == nullptr);
  51.     }
  52.  
  53.     Type &top() const {
  54.         return top_node->get_val();
  55.     }
  56.  
  57.     Stack<Type> &push(Type val) {
  58.         ++cnt;
  59.  
  60.         top_node = new Node<Type>(val, top_node);
  61.  
  62.         return *this;
  63.     }
  64.  
  65.  
  66.     Stack<Type> &pop() {
  67.         if (top_node != nullptr) {
  68.             --cnt;
  69.  
  70.             auto prev = top_node->get_prev();
  71.             delete(top_node);
  72.             top_node = prev;
  73.         }
  74.  
  75.         return *this;
  76.     }
  77.  
  78.     friend std::ostream &operator<<(std::ostream &stream, const Stack<Type> &my_stack) {
  79.         Node<Type> *node = my_stack.top_node;
  80.  
  81.         stream << "< ";
  82.         while (node != nullptr) {
  83.             stream << *node << " ";
  84.             node = node->get_prev();
  85.         }
  86.  
  87.         stream << ">";
  88.  
  89.         return stream;
  90.     }
  91.  
  92.  private:
  93.     Node<Type> *top_node;
  94.     size_t cnt;
  95. };
  96.  
  97. template<typename Type>
  98. class MinStack {
  99.  public:
  100.     size_t size() const {
  101.         return data.size();
  102.     }
  103.  
  104.     bool empty() const {
  105.         return data.empty();
  106.     }
  107.  
  108.     Type &top() const {
  109.         return data.top();
  110.     }
  111.  
  112.     Type &min() const {
  113.         return minimum.top();
  114.     }
  115.  
  116.     MinStack &push(const Type &element) {
  117.         data.push(element);
  118.  
  119.         if (size() > 1 && minimum.top() < element) {
  120.             minimum.push(minimum.top());
  121.         } else {
  122.             minimum.push(element);
  123.         }
  124.  
  125.         return *this;
  126.     }
  127.  
  128.     MinStack &pop() {
  129.         data.pop();
  130.         minimum.pop();
  131.  
  132.         return *this;
  133.     }
  134.  
  135.     friend std::ostream &operator<<(std::ostream &stream, const MinStack &min_stack) {
  136.         if (min_stack.empty()) {
  137.             stream << "<empty>";
  138.         } else {
  139.             stream << "<top = " << min_stack.top() << ", minimum = " << min_stack.min() << ">";
  140.         }
  141.         return stream;
  142.     }
  143.  
  144.  private:
  145.     Stack<Type> data, minimum;
  146. };
  147.  
  148. template<typename Type>
  149. class MinQueue {
  150.  public:
  151.     size_t size() const {
  152.         return head.size() + tail.size();
  153.     }
  154.  
  155.     bool empty() const {
  156.         return (head.empty() && tail.empty());
  157.     }
  158.  
  159.     void shift_if() {
  160.         if (head.empty()) {
  161.             while (!tail.empty()) {
  162.                 head.push(tail.top());
  163.                 tail.pop();
  164.             }
  165.         }
  166.     }
  167.  
  168.     MinQueue &push(const Type &element) {
  169.         tail.push(element);
  170.  
  171.         return *this;
  172.     }
  173.  
  174.     MinQueue &pop() {
  175.         shift_if();
  176.  
  177.         if (!head.empty()) {
  178.             head.pop();
  179.         }
  180.  
  181.         return *this;
  182.     }
  183.  
  184.     Type &top() {
  185.         shift_if();
  186.  
  187.         return head.top();
  188.     }
  189.  
  190.     Type &min() {
  191.         shift_if();
  192.  
  193.         if (tail.empty() || head.min() < tail.min()) {
  194.             return head.min();
  195.         }
  196.  
  197.         return tail.min();
  198.     }
  199.  
  200.  private:
  201.     MinStack<Type> head, tail;
  202. };
  203.  
  204. int main() {
  205.     int n, k, num;
  206.     MinQueue<int> min_queue;
  207.  
  208.     std::cin >> n >> k;
  209.  
  210.     for (int i = 0; i < k; ++i) {
  211.         std::cin >> num;
  212.         min_queue.push(num);
  213.     }
  214.  
  215.     std::cout << min_queue.min() << std::endl;
  216.  
  217.     for (int i = 0; i < n - k; ++i) {
  218.         std::cin >> num;
  219.         min_queue.pop();
  220.         min_queue.push(num);
  221.  
  222.         std::cout << min_queue.min() << std::endl;
  223.     }
  224.     return 0;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement