2
ios::sync_with_stdio(0);7
class MovieRentingSystem {9
#define pppi pair<int, pair<int, int>>11
set<pppi> unrented, rented;13
map<pair<int, int>, int> mp;15
// facilitates search by storing the prices and shops a particular movie is17
unordered_map<int, set<pair<int, int>>> movies;19
MovieRentingSystem(int n, vector<vector<int>> &entries) {20
for (int i = 0; i < entries.size(); i++) {21
int shop = entries[i][0];22
int movie = entries[i][1];23
int price = entries[i][2];25
// initially all movies are unrented26
unrented.insert({price, {shop, movie}});28
// store the price for each pair of {shop,movie}. remember this pair is29
// unique as given in the problem statement30
mp[{shop, movie}] = price;32
// store all the movies with their price and shop33
movies[movie].insert({price, shop});37
vector<int> search(int movie) {38
// return a list of 5 cheapest shops that have the given movie42
for (auto x : movies[movie]) {43
if ((int)ans.size() == 5) break;45
ans.push_back(x.second);51
void rent(int shop, int movie) {52
// remove the movie from the given shop54
int price = mp[{shop, movie}];56
// add this movie in rented57
rented.insert({price, {shop, movie}});59
// remove this movie from unrented60
unrented.erase({price, {shop, movie}});62
// also, erase this shop from the movies, since this movie will be no longer63
// present in the given shop64
movies[movie].erase(movies[movie].find({price, shop}));67
void drop(int shop, int movie) {70
int price = mp[{shop, movie}];72
// add in unrented as movie is back in the shop73
unrented.insert({price, {shop, movie}});75
movies[movie].insert({price, shop});78
rented.erase({price, {shop, movie}});81
vector<vector<int>> report() {82
// return the 5 cheapest rented movies as {shop, movie}84
// here comes the use of rented data structure, since we can quickly select85
// 5 cheapest movies that are rented86
vector<vector<int>> ans;88
for (auto x : rented) {89
if (ans.size() == 5) break;91
int shop = x.second.first;92
int movie = x.second.second;94
ans.push_back({shop, movie});101
* Your MovieRentingSystem object will be instantiated and called as such:102
* MovieRentingSystem* obj = new MovieRentingSystem(n, entries);103
* vector<int> param_1 = obj->search(movie);104
* obj->rent(shop,movie);105
* obj->drop(shop,movie);106
* vector<vector<int>> param_4 = obj->report();