Untitled
unknown
plain_text
9 months ago
2.5 kB
3
Indexable
#include <unordered_map> #include <string> #include <vector> #include <algorithm> using namespace std; struct Product { int cost; bool isBought; } product[30001]; // Không cần hàm so sánh cmp cho unordered_map unordered_map<string, vector<int>> mp; unordered_map<string, vector<int>> mpTag; int n; void init(int N) { n = 0; mp.clear(); mpTag.clear(); // Clear mpTag as well } void addProduct(int mPrice, int tagNum, char tagName[][10]) { Product item; item.cost = mPrice; item.isBought = false; product[n] = item; vector<string> tags; for (int i = 0; i < tagNum; i++) { tags.push_back(tagName[i]); } sort(tags.begin(), tags.end()); // Sắp xếp nhãn để xử lý chính xác // Xử lý các nhóm nhãn (tags) kết hợp for (int i = 0; i < tagNum; i++) { for (int j = i + 1; j < tagNum; j++) { for (int k = j + 1; k < tagNum; k++) { string tempTag = ""; tempTag += tags[i] + tags[j] + tags[k]; // Nối các nhãn lại với nhau mp[tempTag].push_back(n); // Lưu lại id sản phẩm tương ứng } } } // Thêm sản phẩm vào mpTag theo từng tagName for (int i = 0; i < tagNum; i++) { mpTag[tagName[i]].push_back(n); } n++; } int buyProduct(char tag1[], char tag2[], char tag3[]) { int res = -1; vector<string> tagRequire; tagRequire.push_back(tag1); tagRequire.push_back(tag2); tagRequire.push_back(tag3); sort(tagRequire.begin(), tagRequire.end()); // Sắp xếp nhãn theo thứ tự string tempTag = ""; for (auto it : tagRequire) { tempTag += it; } // Kiểm tra xem bộ nhãn (tag) có tồn tại không if (mp.find(tempTag) != mp.end()) { for (auto it : mp[tempTag]) { if (product[it].isBought == false) { product[it].isBought = true; res = product[it].cost; // Trả về giá của sản phẩm được mua break; } } } return res; } void adjustPrice(char tag1[], int changePrice) { // Kiểm tra và điều chỉnh giá của tất cả sản phẩm liên quan đến tag1 if (mpTag.find(tag1) != mpTag.end()) { for (auto it : mpTag[tag1]) { if (product[it].isBought == false) { product[it].cost += changePrice; // Điều chỉnh giá sản phẩm } } } }
Editor is loading...
Leave a Comment