Advertisement
dimon-torchila

Untitled

Jan 8th, 2023
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.69 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. namespace my_namespace {
  6.  
  7.     class queue_empty_exception : public exception {
  8.     public:
  9.         char *what() const throw() {
  10.             return "Error: Queue is empty!";
  11.         }
  12.     };
  13.  
  14.     template<typename T>
  15.     struct queue {
  16.  
  17.         unsigned long long N_op = 0;
  18.  
  19.         queue() : queue_size(0) { N_op++; } // + 1
  20.  
  21.         ~queue() {
  22.             clear(); // 2n + 4
  23.             N_op+= (2 * queue_size + 4);
  24.         }
  25.  
  26.         void clear() { // 2n + 4
  27.             for (node *it = begin; it != nullptr; begin = it) { // 3 + 2n
  28.                 it = begin->next; // + 1
  29.                 delete begin; // + 1
  30.             }
  31.             queue_size = 0; // + 1
  32.  
  33.         }
  34.  
  35.         void push(T value) { // + 8
  36.             if (!queue_size) { // + 1
  37.                 end = new node(value); // + 3
  38.                 begin = end; // + 1
  39.                 N_op+=5;
  40.             } else {
  41.                 end->next = new node(value); // + 4
  42.                 end = end->next; // + 2
  43.                 N_op+=7;
  44.             }
  45.             ++queue_size; // + 1
  46.             N_op++;
  47.         }
  48.  
  49.         void pop() { // + 6
  50.             if (!queue_size) { // + 1
  51.                 throw queue_empty_exception();
  52.             }
  53.             --queue_size; // + 1
  54.             node *next = begin->next; // + 2
  55.             delete begin; // + 1
  56.             begin = next; // + 1
  57.             N_op+=6;
  58.         }
  59.  
  60.         T front() { // + 3
  61.             if (!queue_size) { // + 1
  62.                 throw queue_empty_exception();
  63.             }
  64.             N_op+=3;
  65.             return begin->value; // + 2
  66.         }
  67.  
  68.         bool empty() {
  69.             N_op+=2;
  70.             return queue_size <= 0; // + 2
  71.         }
  72.  
  73.         size_t size() {
  74.             N_op++;
  75.             return queue_size; // + 1
  76.         }
  77.  
  78.     private:
  79.  
  80.         struct node {
  81.             T value;
  82.             node *next;
  83.  
  84.             node(T value, node *next = nullptr) : value(value), next(next) { } //  + 2
  85.         };
  86.  
  87.         size_t queue_size;
  88.         node *begin = nullptr;
  89.         node *end = nullptr;
  90.  
  91.     };
  92.  
  93.     template<typename T>
  94.     void shift_queue(size_t count, queue<T> &q) { // + 3 + 15 * n
  95.         for (size_t i = 0; i < count; ++i) { //  + 3 + 15 * n
  96.             q.push(q.front()); // + 9
  97.             q.pop(); // + 6
  98.         }
  99.         q.N_op += 3;
  100.     }
  101.  
  102.  
  103.     template<typename T>
  104.     ostream &operator<<(ostream &os, queue<T> &q) {
  105.         for (size_t i = 0; i < q.size(); ++i) { // + 4 + 18 * n
  106.             os << q.front() << ' '; // + 3
  107.             q.push(q.front()); // + 9
  108.             q.pop(); // + 6
  109.         }
  110.         q.N_op += 5;
  111.         return os; // + 1
  112.     }
  113.  
  114.     void sort(size_t l, size_t r, queue<int> &q) {
  115.         size_t size = q.size(), mid_index = (r + l) / 2; // + 2 + 3
  116.         shift_queue(mid_index, q); // + 3 + 15 * n
  117.         queue<int> less_q, equal_q, greater_q; // + 3
  118.         int mid_value = q.front(); // + 1 + 3
  119.         equal_q.push(mid_value); // + 6
  120.         q.pop(); // + 6
  121.         for (size_t i = 0; i < r - mid_index - 1; ++i) { // + 4 + n/2 * 19
  122.             if (q.front() > mid_value) // + 4
  123.                 greater_q.push(q.front()); // + 9
  124.             else if (q.front() < mid_value) // + 4
  125.                 less_q.push(q.front()); // + 9
  126.             else
  127.                 equal_q.push(q.front()); // + 9
  128.             q.pop(); // + 6
  129.             q.N_op+=10;
  130.         }
  131.         shift_queue(size - r + l, q); //3 + 15 * n
  132.         for (size_t i = 0; i < mid_index - l; ++i) { // 4 + n/2 * 19
  133.             if (q.front() > mid_value) // + 4
  134.                 greater_q.push(q.front()); // + 9
  135.             else if (q.front() < mid_value) // + 4
  136.                 less_q.push(q.front()); // + 4
  137.             else
  138.                 equal_q.push(q.front()); // + 9
  139.             q.pop(); // + 6
  140.             q.N_op+=10;
  141.         }
  142.         shift_queue(size - r + l, q); // + 3 + 15*n
  143.         size_t l_size = less_q.size(), g_size = greater_q.size(); // + 4
  144.         for (; !less_q.empty();) { // 2 + n * 15
  145.             q.push(less_q.front()); // + 9
  146.             q.N_op+=9;
  147.             less_q.pop(); // + 6
  148.         }
  149.         for (; !equal_q.empty();) { // 2 + 15 * n
  150.             q.push(equal_q.front()); // + 9
  151.             equal_q.pop(); // + 6
  152.             q.N_op+=9;
  153.         }
  154.         for (; !greater_q.empty();) { // + 2 + 15*n
  155.             q.push(greater_q.front()); // + 9
  156.             greater_q.pop(); // + 6
  157.             q.N_op+=9;
  158.         }
  159.         shift_queue(size - r, q); // + 3 + 15* n
  160.         q.N_op += 21;
  161.         if (g_size > 1) // + 1
  162.             sort(r - g_size, r, q);
  163.         if (l_size > 1)
  164.             sort(l, l + l_size, q);
  165.     }
  166. }
  167.  
  168. int main() {
  169.     my_namespace::queue<int> q;
  170.     while (true) {
  171.         int a;
  172.         cin >> a;
  173.         if (a == -1)
  174.             break;
  175.         else if (a == 1) {
  176.             int b;
  177.             cin >> b;
  178.             q.push(b);
  179.         } else if (a == 2) {
  180.             try {
  181.                 q.pop();
  182.             } catch (exception& e) {
  183.                 cout << e.what() << endl;
  184.             }
  185.         } else if (a == 3) {
  186.             try {
  187.                 cout << q.front() << endl;
  188.             } catch (my_namespace::queue_empty_exception& e) {
  189.                 cout << e.what() << endl;
  190.             }
  191.         } else if (a == 4) {
  192.             q.clear();
  193.         } else if (a == 5) {
  194.             cout << q << endl;
  195.             my_namespace::sort(0, q.size(), q);
  196.             cout << q << endl;
  197.         } else if (a == 6) {
  198.             int b;
  199.             cin >> b;
  200.             for (int i = 0; i < b; ++i) {
  201.                 q.push(rand() % 1000);
  202.             }
  203.         }
  204.     }
  205. }
  206.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement