shushus

Спринт 12. RAII в стандартных библиотеках.

Apr 6th, 2023
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <deque>
  3. #include <map>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. struct Client {
  9.     int client_id = 0;
  10.     int room_count = 0;
  11.     int time = 0;
  12. };
  13.  
  14. class HotelManager {
  15. public:
  16.    
  17.     static const int SEC_IN_DAY = 86400;
  18.    
  19.     void Book(int client_id, int room_count, const string& hotel_name, int time) {
  20.         current_time_ = time;
  21.         const Client book{client_id,
  22.                           room_count,
  23.                           time};
  24.        
  25.         auto duplicate_id = [&]() {
  26.             std::deque<Client>& hotel = all_bookings_[hotel_name];
  27.            
  28.             auto it = std::find_if(hotel.begin(),
  29.                                    hotel.end(),
  30.                                    [client_id](const Client& client) {
  31.                                       return client.client_id == client_id;
  32.                                    });
  33.            
  34.            
  35.             if (it == hotel.end()) {
  36.                 all_bookings_[hotel_name].push_back(std::move(book));
  37.             } else {
  38.                 it->room_count += room_count;
  39.                 it->time = time;
  40.             }
  41.         };
  42.         duplicate_id();
  43.     }
  44.  
  45.     int ComputeClientCount(string& hotel_name) {
  46.         int yesterday = current_time_ - SEC_IN_DAY;
  47.         int result = 0;
  48.        
  49.         if (!all_bookings_.count(hotel_name)) {
  50.             return result;
  51.         }
  52.  
  53.         for (auto& [id, _, time] : all_bookings_.at(hotel_name)) {
  54.            
  55.             if (yesterday < time && time <= current_time_) {
  56.                 ++result;
  57.             }
  58.         }
  59.        
  60.         return result;
  61.     }
  62.  
  63.     int ComputeRoomCount(string& hotel_name) {
  64.         int yesterday = current_time_ - SEC_IN_DAY;
  65.         int result = 0;
  66.  
  67.         if (!all_bookings_.count(hotel_name)) {
  68.             return result;
  69.         }
  70.  
  71.         for (auto& [_, room_count, time] : all_bookings_.at(hotel_name)) {
  72.            
  73.             if (yesterday < time && time <= current_time_) {
  74.                 result += room_count;
  75.             }
  76.         }
  77.        
  78.         return result;
  79.     }
  80.  
  81. private:
  82.     int current_time_ = 0;
  83.     map<string, deque<Client>> all_bookings_;
  84. };
  85.  
  86. int main() {
  87.     HotelManager manager;
  88.  
  89.     int query_count;
  90.     cin >> query_count;
  91.  
  92.     for (int query_id = 0; query_id < query_count; ++query_id) {
  93.         string query_type;
  94.         cin >> query_type;
  95.  
  96.         if (query_type == "BOOK") {
  97.             int64_t time;
  98.             cin >> time;
  99.             string hotel_name;
  100.             cin >> hotel_name;
  101.             int client_id, room_count;
  102.             cin >> client_id >> room_count;
  103.             manager.Book(client_id, room_count, hotel_name, time);
  104.         }
  105.         else {
  106.             string hotel_name;
  107.             cin >> hotel_name;
  108.             if (query_type == "CLIENTS") {
  109.                 cout << manager.ComputeClientCount(hotel_name) << "\n";
  110.             }
  111.             else if (query_type == "ROOMS") {
  112.                 cout << manager.ComputeRoomCount(hotel_name) << "\n";
  113.             }
  114.         }
  115.     }
  116.  
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment