shushus

Спринт 6. Амортизированная сложность.

Dec 25th, 2022
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <algorithm>
  2. #include <deque>
  3. #include <string>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. struct Ticket {
  9.     int id;
  10.     string name;
  11. };
  12.  
  13. class TicketOffice {
  14. public:
  15.     void PushTicket(const string& name) {
  16.         last_id_++;
  17.         tickets_.push_back({last_id_, name});
  18.     }
  19.  
  20.     int GetAvailable() const {
  21.         return tickets_.size();
  22.     }
  23.  
  24.     int GetAvailable(const string& name) const {
  25.         return count_if(begin(tickets_), end(tickets_), [name](const Ticket& T){return name==T.name;});
  26.     }
  27.  
  28.     void Invalidate(int minimum) {
  29.         while (!tickets_.empty() && tickets_.front().id <= minimum){
  30.             tickets_.pop_front();
  31.         }
  32.     }
  33. private:
  34.     int last_id_ = 0;
  35.     deque<Ticket> tickets_;
  36. };
  37.  
  38. int main(){
  39.     TicketOffice tickets;
  40.  
  41.     tickets.PushTicket("Swan Lake");
  42.     tickets.PushTicket("Swan Lake");
  43.     tickets.PushTicket("Boris Godunov");
  44.     tickets.PushTicket("Boris Godunov");
  45.     tickets.PushTicket("Swan Lake");
  46.     tickets.PushTicket("Boris Godunov");
  47.     tickets.PushTicket("Boris Godunov");
  48.  
  49.     cout << tickets.GetAvailable() << endl;
  50.     cout << tickets.GetAvailable("Swan Lake") << endl;
  51.     cout << tickets.GetAvailable("Boris Godunov") << endl;
  52.  
  53.     tickets.Invalidate(3);
  54.  
  55.     cout << tickets.GetAvailable() << endl;
  56.     cout << tickets.GetAvailable("Swan Lake") << endl;
  57.     cout << tickets.GetAvailable("Boris Godunov") << endl;
  58.  
  59.     tickets.PushTicket("Swan Lake");
  60.     tickets.PushTicket("Swan Lake");
  61.  
  62.     cout << tickets.GetAvailable("Swan Lake") << endl;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment