Advertisement
Guest User

red_booking

a guest
Jul 17th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <queue>
  4. #include <map>
  5. #include <algorithm>
  6. #include <stdexcept>
  7.  
  8. using namespace std;
  9.  
  10. class BookManager {
  11. public:
  12.     BookManager() {
  13.  
  14.     }
  15.     void Book(const string& hotel_name, const long long& time,
  16.             const int& client_id, const int& room_count) {
  17.         last_time = time;
  18.         rooms[hotel_name].push(make_pair(time, room_count));
  19.         room_amount[hotel_name]+=room_count;
  20.         clients_[hotel_name][client_id]+=room_count;
  21.                  //.push(make_pair(client_id, time));
  22.         if(rooms[hotel_name].size() > 1) {
  23.             long long current_time = rooms[hotel_name].back().first;
  24.             while(rooms[hotel_name].front().first <= current_time - 86400) {
  25.                 /*for(auto& a : room_amount) {
  26.                     a.second-=rooms.at(a.first).front().second;
  27.                 }*/
  28.                 room_amount[hotel_name]-=rooms[hotel_name].front().second;
  29.                 clients_[hotel_name][client_id]-=rooms[hotel_name].front().second;
  30.                 if(clients_[hotel_name][client_id] == 0) {
  31.                     clients_[hotel_name].erase(client_id);
  32.                 }
  33.                 rooms[hotel_name].pop();
  34.             }
  35.         }
  36.  
  37.     }
  38.     int Clients(const string& hotel_name) const {
  39.         //vector<pair<long long, int>> v;
  40.         //clients.at(hotel_name);
  41.         try {
  42.             return clients_.at(hotel_name).size();
  43.         } catch(exception& e) {
  44.             return 0;
  45.         }
  46.     }
  47.     int Rooms(const string& hotel_name) {
  48.         //if(room_amount[hotel_name] == 0) {return 0;}
  49.         try {
  50.         //if(rooms[hotel_name].size() > 0) {
  51.             long long current_time = last_time;
  52.             //cout << "really?" << rooms[hotel_name].front().first << ", " << current_time - 86400 << endl;
  53.             while(rooms.at(hotel_name).front().first <= current_time - 86400) {
  54.                 //cout << hotel_name << "!" << endl;
  55.                 room_amount.at(hotel_name)-=rooms.at(hotel_name).front().second;
  56.                 rooms.at(hotel_name).pop();
  57.             }
  58.         //}
  59.         return room_amount.at(hotel_name);
  60.         } catch(exception& e) {
  61.             return 0;
  62.         }
  63.     }
  64. private:
  65.     map<string, queue<pair<long long, int>>> rooms;
  66.     //map<string, queue<pair<long long, int>>> clients;
  67.     map<string, map<int, int>> clients_;
  68.     map<string, int> room_amount;
  69.     long long last_time;
  70.     //map<string, int> clients_amount;
  71.     //int room_amount;
  72.     //int clients_amount;
  73. };
  74.  
  75. int main() {
  76.     ios::sync_with_stdio(false);
  77.     cin.tie(nullptr);
  78.  
  79.     BookManager bm;
  80.     int query_count;
  81.     cin >> query_count;
  82.     for(int i = 0; i < query_count; ++i) {
  83.         string query_type, hotel_name;
  84.         cin >> query_type;
  85.         if(query_type == "BOOK") {
  86.             long long time;
  87.             int client_id;
  88.             int room_count;
  89.             cin >> time >> hotel_name >> client_id >> room_count;
  90.             bm.Book(hotel_name, time, client_id, room_count);
  91.         } else if(query_type == "CLIENTS") {
  92.             cin >> hotel_name;
  93.             cout << bm.Clients(hotel_name) << endl;
  94.         } else if(query_type == "ROOMS") {
  95.             cin >> hotel_name;
  96.             cout << bm.Rooms(hotel_name) << endl;
  97.         }
  98.     }
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement