1
+ // Runtime: 1596 ms (Top 70.59%) | Memory: 410.3 MB (Top 60.51%)
2
+
1
3
int speedup = []{ios::sync_with_stdio (0 ); cin.tie (0 ); return 0 ;}();
2
4
3
5
class MovieRentingSystem {
4
6
public:
5
-
6
- #define pppi pair<int , pair <int , int >>
7
-
7
+
8
+ #define pppi pair<int , pair <int , int >>
9
+
8
10
set < pppi > unrented,rented;
9
-
11
+
10
12
map < pair <int , int >, int > mp;
11
-
13
+
12
14
// facilitates search by storing the prices and shops a particular movie is associated with
13
15
unordered_map <int , set < pair <int , int > > > movies;
14
-
16
+
15
17
MovieRentingSystem (int n, vector<vector<int >>& entries) {
16
-
18
+
17
19
for (int i = 0 ; i < entries.size (); i++)
18
20
{
19
21
int shop = entries[i][0 ];
20
22
int movie = entries[i][1 ];
21
23
int price = entries[i][2 ];
22
-
24
+
23
25
// initially all movies are unrented
24
26
unrented.insert ({price, {shop, movie}});
25
-
27
+
26
28
// store the price for each pair of {shop,movie}. remember this pair is unique as given in the problem statement
27
29
mp[{shop,movie}] = price;
28
-
30
+
29
31
// store all the movies with their price and shop
30
32
movies[movie].insert ({price,shop});
31
33
}
32
34
}
33
-
35
+
34
36
vector<int > search (int movie) {
35
-
37
+
36
38
// return a list of 5 cheapest shops that have the given movie
37
-
39
+
38
40
vector <int > ans;
39
-
41
+
40
42
for (auto x : movies[movie])
41
43
{
42
44
if ((int )ans.size ()==5 )
43
45
break ;
44
-
46
+
45
47
ans.push_back (x.second );
46
48
}
47
-
49
+
48
50
return ans;
49
51
}
50
-
52
+
51
53
void rent (int shop, int movie) {
52
54
// remove the movie from the given shop
53
-
55
+
54
56
int price = mp[{shop,movie}];
55
-
57
+
56
58
// add this movie in rented
57
59
rented.insert ({price, {shop, movie}});
58
-
60
+
59
61
// remove this movie from unrented
60
- unrented.erase ({price, {shop, movie}});
61
-
62
+ unrented.erase ({price, {shop, movie}});
63
+
62
64
// also, erase this shop from the movies, since this movie will be no longer present in the given shop
63
65
movies[movie].erase (movies[movie].find ({price,shop}));
64
66
}
65
-
67
+
66
68
void drop (int shop, int movie) {
67
69
// return or add
68
-
70
+
69
71
int price = mp[{shop,movie}];
70
72
71
73
// add in unrented as movie is back in the shop
72
74
unrented.insert ({price, {shop, movie}});
73
-
75
+
74
76
movies[movie].insert ({price,shop});
75
-
77
+
76
78
// remove from rented
77
- rented.erase ({price, {shop, movie}});
79
+ rented.erase ({price, {shop, movie}});
78
80
}
79
-
81
+
80
82
vector<vector<int >> report () {
81
83
// return the 5 cheapest rented movies as {shop, movie}
82
-
83
- // here comes the use of rented data structure, since we can quickly select 5 cheapest movies that are rented
84
+
85
+ // here comes the use of rented data structure, since we can quickly select 5 cheapest movies that are rented
84
86
vector<vector<int >> ans;
85
-
87
+
86
88
for (auto x : rented)
87
89
{
88
90
if (ans.size () == 5 )
89
91
break ;
90
-
91
- int shop = x.second .first ;
92
+
93
+ int shop = x.second .first ;
92
94
int movie = x.second .second ;
93
-
95
+
94
96
ans.push_back ({shop,movie});
95
97
}
96
98
return ans;
@@ -104,4 +106,4 @@ class MovieRentingSystem {
104
106
* obj->rent(shop,movie);
105
107
* obj->drop(shop,movie);
106
108
* vector<vector<int>> param_4 = obj->report();
107
- */
109
+ */
0 commit comments