Advertisement
Guest User

red-w2-booking

a guest
Aug 17th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. // red-w2-booking
  2. #include <iostream>
  3. #include <vector>
  4. #include <deque>
  5. #include <map>
  6. #include <string>
  7. #include <sstream>
  8. #include <fstream>
  9. #include <algorithm>
  10. #include <unordered_set>
  11.  
  12. #include "profile.h"
  13.  
  14. using namespace std;
  15.  
  16. struct Booking {
  17.   long long time;
  18.   int roomsCount;
  19. };
  20.  
  21. class BookingManager {
  22. public:
  23.   void book(long long time, const string& hotel, int client, int roomsCount) {
  24.     current_time = time;
  25.     entries[hotel].push_back({time, roomsCount});
  26.     clientLastAccess[hotel][client] = time;
  27.     roomCounts[hotel] += roomsCount;
  28.   }
  29.  
  30.   int clientsWithinLastDay(const string& hotel) {
  31.     auto& clients = clientLastAccess[hotel];
  32.     for (auto it = clients.begin(); it != clients.end(); ) {
  33.       if (current_time - it->second < SECONDS_IN_DAY) {
  34.         ++it;
  35.       } else {
  36.         clients.erase(it++);
  37.       }
  38.     }
  39.     return static_cast<int>(clients.size());
  40.   }
  41.  
  42.   int roomsBookedWithinLastDay(const string& hotel) {
  43.     const auto& hotelEntries = entries[hotel];
  44.     int& start = startPositions[hotel];
  45.     int& roomsCount = roomCounts[hotel];
  46.     for (auto it = hotelEntries.begin() + start; it < hotelEntries.end() ; ++it) {
  47.       if (current_time - it->time >= SECONDS_IN_DAY) {
  48.         ++start;
  49.         roomsCount -= it->roomsCount;
  50.       } else {
  51.         break;
  52.       }
  53.     }
  54.     return roomsCount;
  55.   }
  56.  
  57. private:
  58.   long long current_time = 0;
  59.   static const long long SECONDS_IN_DAY = 24 * 3600;
  60.   map<string, vector<Booking>> entries;
  61.   map<string, int> startPositions;
  62.   map<string, int> roomCounts;
  63.   map<string, map<int, long long>> clientLastAccess;
  64. };
  65.  
  66. int main() {
  67.   ios::sync_with_stdio(false);
  68.   cin.tie(nullptr);
  69.  
  70. //  {
  71. //    BookingManager manager;
  72. //    LOG_DURATION("book");
  73. //    for (int i = 0; i < 100000; ++i) {
  74. //      manager.book(i, "bulka", 2*i + 3, 3);
  75. //    }
  76. //  }
  77. //  return 0;
  78.  
  79. #ifdef __APPLE__
  80.   auto cin = ifstream("bookings.txt");
  81.   auto cout = ostringstream();
  82. #endif
  83.  
  84.   BookingManager manager;
  85.   int query_count;
  86.   cin >> query_count;
  87.  
  88.   string command;
  89.   for (int i = 0; i < query_count; ++i) {
  90.     cin >> command;
  91.     if (command == "BOOK") {
  92.       long long time;
  93.       string hotel;
  94.       int client, roomsCount;
  95.       cin >> time >> hotel >> client >> roomsCount;
  96.       manager.book(time, hotel, client, roomsCount);
  97.     } else if (command == "CLIENTS") {
  98.       string hotel;
  99.       cin >> hotel;
  100.       cout << manager.clientsWithinLastDay(hotel) << '\n';
  101.     } else if (command == "ROOMS") {
  102.       string hotel;
  103.       cin >> hotel;
  104.       cout << manager.roomsBookedWithinLastDay(hotel) << '\n';
  105.     }
  106.   }
  107.  
  108. #ifdef __APPLE__
  109.   cerr << (cout.str() == "3\n1\n27\n2\n59\n3\n122\n3\n248\n3\n496\n2\n483\n1\n0\n0\n1\n1\n3\n2\n") << endl;
  110. #endif
  111.  
  112.   return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement