Advertisement
pb_jiang

LC1912 TLE

Nov 14th, 2022 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. class MovieRentingSystem
  2. {
  3.     template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  4.     using pii = pair<int, int>;
  5.     using piii = pair<pii, int>;
  6.     unordered_map<int, mpq<pii>> mv_pq; // movie, <price, shop>
  7.  
  8.     mpq<piii> rented; // <<price, shop>, movie>
  9.  
  10.     set<piii> instore; // <<price, shop>, movie>
  11.  
  12.     map<pii, int> pmap; // <<shop, movie>, price>
  13.  
  14.   public:
  15.     MovieRentingSystem(int n, vector<vector<int>> &entries)
  16.     {
  17.         for (const auto &e : entries) {
  18.             // e: shop, movie, price
  19.             mv_pq[e[1]].push({e[2], e[0]});
  20.             instore.insert({{e[2], e[0]}, e[1]});
  21.             pmap[pii(e[0], e[1])] = e[2];
  22.         }
  23.     }
  24.  
  25.     vector<int> search(int movie)
  26.     {
  27.         mpq<pii> &pq = mv_pq[movie];
  28.         set<pii> rem;
  29.         vector<int> ret;
  30.         while (pq.empty() == false && ret.size() < 5) {
  31.             auto h = pq.top();
  32.             pq.pop();
  33.             piii item{h, movie};
  34.             if (instore.count(item) && rem.count(h) == 0) {
  35.                 ret.push_back(h.second);
  36.                 rem.insert(h);
  37.             }
  38.         }
  39.         for (const auto &val : rem)
  40.             pq.push(val);
  41.         return ret;
  42.     }
  43.  
  44.     void rent(int shop, int movie)
  45.     {
  46.         int price = pmap[pii(shop, movie)];
  47.         piii item{{price, shop}, movie};
  48.         instore.erase(item);
  49.         rented.push(item);
  50.     }
  51.  
  52.     void drop(int shop, int movie)
  53.     {
  54.         int price = pmap[pii(shop, movie)];
  55.         piii item{{price, shop}, movie};
  56.         // unordered_map<int, mpq<pii>> mv_pq; // movie, <price, shop>
  57.         mv_pq[movie].push({price, shop});
  58.         instore.insert(item);
  59.     }
  60.  
  61.     vector<vector<int>> report()
  62.     {
  63.         // mpq<piii> rented;  // <<price, shop>, movie>
  64.         vector<vector<int>> ret;
  65.         set<piii> rem;
  66.         while (rented.size() && ret.size() < 5) {
  67.             auto h = rented.top();
  68.             rented.pop();
  69.             if (instore.count(h) == 0 && rem.count(h) == 0) {
  70.                 ret.push_back({h.first.second, h.second});
  71.                 rem.insert(h);
  72.             }
  73.         }
  74.         for (const auto &val : rem)
  75.             rented.push(val);
  76.         return ret;
  77.     }
  78. };
  79.  
  80. /**
  81.  * Your MovieRentingSystem object will be instantiated and called as such:
  82.  * MovieRentingSystem* obj = new MovieRentingSystem(n, entries);
  83.  * vector<int> param_1 = obj->search(movie);
  84.  * obj->rent(shop,movie);
  85.  * obj->drop(shop,movie);
  86.  * vector<vector<int>> param_4 = obj->report();
  87.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement