Advertisement
coloriot

HA14_classes

Jul 12th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. // Struct -> classes
  4.  
  5. class Node {
  6. public:
  7.     Node(int val) : value(val), next(nullptr), prev(nullptr) {}
  8.     int getValue() const { return value; }
  9.     Node* getNext() const { return next; }
  10.     void setNext(Node* nextNode) { next = nextNode; }
  11.     Node* getPrev() const { return prev; }
  12.     void setPrev(Node* prevNode) { prev = prevNode; }
  13.  
  14. private:
  15.     int value;
  16.     Node* next;
  17.     Node* prev;
  18. };
  19.  
  20. class List {
  21. public:
  22.     List() : start(nullptr), finish(nullptr), size(0) {}
  23.  
  24.     void push_back(int new_value) {
  25.         Node* new_node = new Node(new_value);
  26.         if (size > 0) {
  27.             finish->setNext(new_node);
  28.             new_node->setPrev(finish);
  29.         } else {
  30.             start = new_node;
  31.         }
  32.         finish = new_node;
  33.         ++size;
  34.     }
  35.  
  36.     void push_front(int new_value) {
  37.         Node* new_node = new Node(new_value);
  38.         new_node->setNext(start);
  39.         if (start != nullptr) {
  40.             start->setPrev(new_node);
  41.         }
  42.         start = new_node;
  43.         if (size == 0) {
  44.             finish = new_node;
  45.         }
  46.         ++size;
  47.     }
  48.  
  49.     void pop_front() {
  50.         if (size == 0) {
  51.             return;
  52.         }
  53.         Node* old_start = start;
  54.         start = start->getNext();
  55.         if (start != nullptr) {
  56.             start->setPrev(nullptr);
  57.         } else {
  58.             finish = nullptr;
  59.         }
  60.         delete old_start;
  61.         --size;
  62.     }
  63.  
  64.     void pop_back() {
  65.         if (size == 0) {
  66.             return;
  67.         }
  68.         Node* old_finish = finish;
  69.         finish = finish->getPrev();
  70.         if (finish != nullptr) {
  71.             finish->setNext(nullptr);
  72.         } else {
  73.             start = nullptr;
  74.         }
  75.         delete old_finish;
  76.         --size;
  77.     }
  78.  
  79.     Node* getStart() const { return start; }
  80.     int getSize() const { return size; }
  81.  
  82. private:
  83.     Node* start;
  84.     Node* finish;
  85.     int size;
  86. };
  87.  
  88. void process_operation(List* compartments, char op, int index, int value, int& current_total, int& max_total) {
  89.     if (op == '+') {
  90.         compartments[index - 1].push_back(value);
  91.         current_total++;
  92.     } else if (op == '-') {
  93.         if (compartments[index - 1].getStart() != nullptr && compartments[index - 1].getStart()->getValue() == value) {
  94.             compartments[index - 1].pop_front();
  95.             current_total--;
  96.         }
  97.     }
  98.  
  99.     if (current_total > max_total) {
  100.         max_total = current_total;
  101.     }
  102. }
  103.  
  104. int main() {
  105.     int N, K, P;
  106.     std::cin >> N >> K >> P;
  107.     std::cin.ignore();
  108.  
  109.     List* compartments = new List[N];
  110.  
  111.     char op;
  112.     int index, value;
  113.     int current_total = 0;
  114.     int max_total = 0;
  115.  
  116.     for (int i = 0; i < P; ++i) {
  117.         std::cin >> op >> index >> value;
  118.         process_operation(compartments, op, index, value, current_total, max_total);
  119.     }
  120.  
  121.     std::cout << max_total << std::endl;
  122.  
  123.     for (int i = 0; i < N; ++i) {
  124.         while (compartments[i].getSize() > 0) {
  125.             compartments[i].pop_front();
  126.         }
  127.     }
  128.  
  129.     delete[] compartments;
  130.     return 0;
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement