Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MovieRentingSystem
- {
- template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
- using pii = pair<int, int>;
- using piii = pair<pii, int>;
- unordered_map<int, mpq<pii>> mv_pq; // movie, <price, shop>
- mpq<piii> rented; // <<price, shop>, movie>
- set<piii> instore; // <<price, shop>, movie>
- map<pii, int> pmap; // <<shop, movie>, price>
- public:
- MovieRentingSystem(int n, vector<vector<int>> &entries)
- {
- for (const auto &e : entries) {
- // e: shop, movie, price
- mv_pq[e[1]].push({e[2], e[0]});
- instore.insert({{e[2], e[0]}, e[1]});
- pmap[pii(e[0], e[1])] = e[2];
- }
- }
- vector<int> search(int movie)
- {
- mpq<pii> &pq = mv_pq[movie];
- set<pii> rem;
- vector<int> ret;
- while (pq.empty() == false && ret.size() < 5) {
- auto h = pq.top();
- pq.pop();
- piii item{h, movie};
- if (instore.count(item) && rem.count(h) == 0) {
- ret.push_back(h.second);
- rem.insert(h);
- }
- }
- for (const auto &val : rem)
- pq.push(val);
- return ret;
- }
- void rent(int shop, int movie)
- {
- int price = pmap[pii(shop, movie)];
- piii item{{price, shop}, movie};
- instore.erase(item);
- rented.push(item);
- }
- void drop(int shop, int movie)
- {
- int price = pmap[pii(shop, movie)];
- piii item{{price, shop}, movie};
- // unordered_map<int, mpq<pii>> mv_pq; // movie, <price, shop>
- mv_pq[movie].push({price, shop});
- instore.insert(item);
- }
- vector<vector<int>> report()
- {
- // mpq<piii> rented; // <<price, shop>, movie>
- vector<vector<int>> ret;
- set<piii> rem;
- while (rented.size() && ret.size() < 5) {
- auto h = rented.top();
- rented.pop();
- if (instore.count(h) == 0 && rem.count(h) == 0) {
- ret.push_back({h.first.second, h.second});
- rem.insert(h);
- }
- }
- for (const auto &val : rem)
- rented.push(val);
- return ret;
- }
- };
- /**
- * Your MovieRentingSystem object will be instantiated and called as such:
- * MovieRentingSystem* obj = new MovieRentingSystem(n, entries);
- * vector<int> param_1 = obj->search(movie);
- * obj->rent(shop,movie);
- * obj->drop(shop,movie);
- * vector<vector<int>> param_4 = obj->report();
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement