nochnoikoshmar1

Untitled

May 1st, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.69 KB | None | 0 0
  1. #include <cmath>
  2. #include <map>
  3. #include <string>
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <set>
  7. #include <utility>
  8. #include <vector>
  9. #include <numeric>
  10. #include <chrono>
  11. #include <type_traits>
  12. #include <cassert>
  13. #include <sstream>
  14. #include <unordered_map>
  15. #include <stdexcept>
  16. #include <stack>
  17. #include <iterator>
  18. #include <iomanip>
  19. using namespace std;
  20.  
  21. template <typename RandomIt>
  22. void random_shuffle(RandomIt first, RandomIt last){
  23.     typename iterator_traits<RandomIt>::difference_type i, n;
  24.     n = last - first;
  25.     for(i = n - 1; i > 0; --i){
  26.         swap(first[i], first[rand() % (i + 1)]);
  27.     }
  28. }
  29.  
  30. template <typename Type>
  31. struct PairWithMin{Type element;Type minimum;};
  32. // будем выводить только сам элемент, выводить минимум не будем
  33. template <typename Type>
  34. ostream &operator<<(ostream &out, const PairWithMin<Type> &pair_to_print){return out << pair_to_print.element;}
  35.  
  36. template <typename It>
  37. void PrintRange(It range_begin, It range_end){
  38.     for(auto it = range_begin; it != range_end; ++it){
  39.         cout << *it << " "s;
  40.     }
  41.     cout << endl;
  42. }
  43.  
  44. template <typename Type>
  45. class Stack{
  46. public:  
  47.     //Вставка элемента в стек
  48.     void Push(const Type &element){elements_.push_back(element);}
  49.     //Удаление элемента из стека
  50.     void Pop(){elements_.pop_back();}
  51.     //Получение значения вершины стека конст
  52.     const Type &Peek() const{return elements_.back();}
  53.     //Получение значения вершины стека
  54.     Type &Peek(){return elements_.back();}
  55.     // вывод в консоль
  56.     void Print() const{ PrintRange(elements_.begin(), elements_.end());}
  57.     //Размер стека — Size
  58.     uint64_t Size() const{return elements_.size();}
  59.     //Проверка на пустоту
  60.     bool IsEmpty() const{return elements_.empty();}
  61.     const Type &Front() const{return elements_.front();}
  62.     Type &Front(){return elements_.front();}    
  63.     ~Stack(){};
  64. private:
  65.     vector<Type> elements_;
  66. };
  67.  
  68. template <typename Type>
  69. class StackMin{
  70. public:    
  71.     void Push(const Type &element){
  72.         /*if(!minimum_.IsEmpty()){
  73.             if(element > minimum_.Front().second){
  74.                 minimum_.Push({element, minimum_.Front().second});
  75.             }
  76.         } else{
  77.             minimum_.Push({element,element});
  78.         }*/
  79. //_________________________________________________________________
  80.         // проверим, что должно стать минимумом для текущего элемента -  он сам, или предыдущий минимум
  81.         PairWithMin<Type> new_pair = {element, element};        
  82.         if(!minimum_.IsEmpty() && new_pair.minimum > minimum_.Peek().minimum){
  83.             new_pair.minimum = minimum_.Peek().minimum;
  84.         }        
  85.         minimum_.Push(new_pair); // положим структуру в стек
  86.     }
  87.     void Pop(){minimum_.Pop();}
  88.     const Type &Peek() const{return minimum_.Peek().element;}
  89.     Type &Peek(){return minimum_.Peek().element;}
  90.     void Print() const{minimum_.Print();}
  91.     uint64_t Size() const{return minimum_.Size();}
  92.     bool IsEmpty() const{return minimum_.IsEmpty();}
  93.     const Type PeekMin() const{return minimum_.Peek().minimum;}
  94.     Type PeekMin(){return minimum_.Peek().minimum;}
  95.     ~StackMin(){};
  96. private:
  97.     Stack<PairWithMin<Type>> minimum_;  
  98. };
  99.  
  100. template <typename Type>
  101. class SortedStack{
  102. public:
  103.     void Push(const Type &element){
  104.         if(elements_.IsEmpty()){
  105.             elements_.Push(element);
  106.             return;
  107.         }
  108.         Type last_elem = elements_.Peek();
  109.         if(last_elem < element){
  110.             elements_.Pop();
  111.             Push(element);
  112.             elements_.Push(last_elem);
  113.         }else{
  114.             elements_.Push(element);
  115.         }
  116.     }
  117.     void Pop(){elements_.Pop();}
  118.     const Type &Peek() const{return elements_.Peek();}
  119.     Type &Peek(){return elements_.Peek();}
  120.     void Print() const{elements_.Print();}
  121.     uint64_t Size() const{return elements_.Size();}
  122.     bool IsEmpty() const{return elements_.IsEmpty();}    
  123.     ~SortedStack(){};
  124. private:
  125.     Stack<Type> elements_;
  126. };
  127.  
  128. template <typename Type>
  129. class Queue{
  130. public:
  131.     void Push(const Type &element){
  132.         //if(stack1_.empty()){
  133.             stack1_.push(element);
  134.        //     return;
  135.        // }
  136.     }
  137.     //void Pop(){
  138.     //    if(!stack1_.empty()){
  139.     //        Type last_elem = stack1_.top();
  140.     //        if(last_elem < stack2_.top()){
  141.     //            //stack1_.pop();
  142.     //            //Push(last_elem);
  143.     //            stack2_.push(last_elem);
  144.     //        } else{
  145.     //            stack1_.push(stack2_.top());
  146.     //        }
  147.     //    }
  148.     //    /*if(stack2_.empty()){
  149.     //        while(!stack1_.empty()){
  150.     //            Type last_elem = stack1_.top();
  151.     //            stack1_.pop();
  152.     //            stack2_.push(last_elem);                
  153.     //        }
  154.     //    }*/
  155.     //}
  156.     void Pop(){
  157.         if(stack2_.empty()){
  158.             while(!stack1_.empty()){
  159.                 stack2_.push(stack1_.top());
  160.                 stack1_.pop();
  161.             }
  162.         }
  163.         stack2_.pop();
  164.     }
  165.     Type &Front(){
  166.         if(stack2_.empty()){
  167.             while(!stack1_.empty()){
  168.                 stack2_.push(stack1_.top());
  169.                 stack1_.pop();
  170.             }
  171.         }
  172.         return stack2_.top();
  173.     }
  174.     uint64_t Size() const{return stack1_.size() + stack2_.size();}
  175.     bool IsEmpty() const{return stack1_.empty() && stack2_.empty();}
  176.  
  177. private:
  178.     stack<Type> stack1_;
  179.     stack<Type> stack2_;
  180. };
Advertisement
Add Comment
Please, Sign In to add comment