1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| #include <set> #include <unordered_map> #include <vector>
using std::set; using std::unordered_map; using std::vector;
class Movie { public: int shop; int movie; int price;
bool operator<(const Movie &other) const { if (price != other.price) return price < other.price; if (shop != other.shop) return shop < other.shop; return movie < other.movie; } };
class MovieRentingSystem { private: unordered_map<int, unordered_map<int, int>> dmap; unordered_map<int, set<Movie>> umap; set<Movie> rented; set<Movie> unrented;
int shop_num;
public: MovieRentingSystem(int n, vector<vector<int>> &entries) { int shop, movie, price; shop_num = n; for (vector<int> vec : entries) { shop = vec[0]; movie = vec[1]; price = vec[2]; dmap[movie][shop] = price; unrented.insert({shop, movie, price}); umap[movie].insert({{shop, movie, price}}); } } vector<int> search(int movie) { vector<int> res; set<Movie> &tmp_set = umap[movie]; int cnt = 0; for (auto it = tmp_set.begin(); it != tmp_set.end() && cnt < 5; it++, cnt++) { res.push_back(it->shop); } return res; }
void rent(int shop, int movie) { int price = dmap[movie][shop]; rented.insert({shop, movie, price}); unrented.erase({shop, movie, price}); umap[movie].erase({shop, movie, price}); if (umap[movie].empty()) { umap.erase(movie); } } void drop(int shop, int movie) { int price = dmap[movie][shop]; unrented.insert({shop, movie, price}); rented.erase({shop, movie, price}); umap[movie].insert({shop, movie, price}); } vector<vector<int>> report() { vector<vector<int>> res; int cnt = 0; for (auto it = rented.begin(); it != rented.end() && cnt < 5; it++, cnt++) { vector<int> vec; vec.push_back(it->shop); vec.push_back(it->movie); res.push_back(vec); } return res; } };
#include <iostream> #include <string> using std::cout; using std::endl; using std::string;
static string vec_to_str(const vector<int> &v) { string s = "["; for (size_t i = 0; i < v.size(); ++i) { s += std::to_string(v[i]); if (i + 1 != v.size()) s += ","; } s += "]"; return s; } static string mat_to_str(const vector<vector<int>> &m) { string s = "["; for (size_t i = 0; i < m.size(); ++i) { s += vec_to_str(m[i]); if (i + 1 != m.size()) s += ", "; } s += "]"; return s; }
int main() { vector<vector<int>> entries = {{0, 1, 5}, {0, 2, 6}, {0, 3, 7}, {1, 1, 4}, {1, 2, 7}, {2, 1, 5}}; vector<string> outputs; outputs.push_back("null"); MovieRentingSystem obj(3, entries);
outputs.push_back(vec_to_str(obj.search(1))); obj.rent(0, 1); outputs.push_back("null"); obj.rent(1, 2); outputs.push_back("null"); outputs.push_back(mat_to_str(obj.report())); obj.drop(1, 2); outputs.push_back("null"); outputs.push_back(vec_to_str(obj.search(2)));
cout << "["; for (size_t i = 0; i < outputs.size(); ++i) { cout << outputs[i]; if (i + 1 != outputs.size()) cout << ", "; } cout << "]\n"; return 0; }
|