Advertisement
RobertDeMilo

RB2.13 Амортизированная сложность

Apr 16th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdint>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. class EventManager
  8. {
  9. public:
  10.    
  11.     void Add(uint64_t time) // amortized O(1)
  12.     {
  13.         Adjust(time);
  14.         events.push(time); // O(1)
  15.     }
  16.    
  17.     int Count(uint64_t time) // amortized O(1)
  18.     {
  19.         Adjust(time);
  20.         return events.size(); // O(1)
  21.     }
  22.    
  23. private:
  24.    
  25.     queue<int64_t> events;
  26.  
  27.     void Adjust(uint64_t time) // amortized O(1)
  28.     {
  29.         while(!events.empty()&&events.front()<=time-300) // O(Q)
  30.         {
  31.             events.pop(); // O(1)
  32.         }
  33.     }
  34. };
  35.  
  36. int main()
  37. {
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement