Untitled
unknown
plain_text
a year ago
3.0 kB
6
Indexable
#include <unordered_map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
struct Product {
int rank;
int score;
bool deleted;
};
const int MAX_CATEGORIES = 6;
const int MAX_SCORE = 101;
unordered_map<int, Product> global[MAX_CATEGORIES];
unordered_map<int, int> global_all;
set<pair<int, int>> prod_data[MAX_CATEGORIES]; // set<rank, GoodsID>
int rank_map[MAX_SCORE];
void init() {
for (int i = 0; i < MAX_SCORE; ++i) {
rank_map[i] = i <= 19 ? 5 : i <= 39 ? 4 : i <= 59 ? 3 : i <= 79 ? 2 : 1;
}
for (int i = 1; i < MAX_CATEGORIES; ++i) {
global[i].clear();
prod_data[i].clear();
}
global_all.clear();
}
void add(int mGoodsID, int mCategory, int mScore) {
// Product temp{rank_map[mScore], mScore, false};
Product temp;
temp.rank = rank_map[mScore];
temp.score = mScore;
temp.deleted = false;
global[mCategory][mGoodsID] = temp;
global_all[mGoodsID] = mCategory;
prod_data[mCategory].emplace(temp.rank, mGoodsID);
}
void remove(int mGoodsID) {
global[global_all[mGoodsID]][mGoodsID].deleted = true;
}
void updateRank(int mGoodsID, int newScore) {
int mCategory = global_all[mGoodsID];
Product& prod = global[mCategory][mGoodsID];
int oldRank = prod.rank;
prod.score = newScore;
prod.rank = rank_map[newScore];
if (oldRank != prod.rank) {
prod_data[mCategory].erase(make_pair(oldRank, mGoodsID));
prod_data[mCategory].emplace(prod.rank, mGoodsID);
}
}
void purchase(int mGoodsID) {
int newScore = min(global[global_all[mGoodsID]][mGoodsID].score + 5, 100);
updateRank(mGoodsID, newScore);
}
void takeBack(int mGoodsID) {
int newScore = max(global[global_all[mGoodsID]][mGoodsID].score - 10, 0);
updateRank(mGoodsID, newScore);
}
void changeScore(int mCategory, int mChangeScore) {
for (auto& it : global[mCategory]) {
if (!it.second.deleted) {
int newScore = it.second.score + mChangeScore;
if(newScore > 100) newScore = 100;
else if(newScore < 0) newScore = 0;
updateRank(it.first, newScore);
}
}
}
int getTopRank(int mCategory) {
if (mCategory == 0) {
pair<int, int> ans;
ans.first = 5;
ans.second = 0;
for (int i = 1; i < MAX_CATEGORIES; ++i) {
for (const auto& it : prod_data[i]) {
if (!global[i][it.second].deleted) {
if (it.first < ans.first || (it.first == ans.first && it.second > ans.second)) {
ans.first = it.first;
ans.second = it.second;
}
}
}
}
return ans.second;
} else {
for (const auto& it : prod_data[mCategory]) {
if (!global[mCategory][it.second].deleted) {
return it.second;
}
}
}
return 0;
}Editor is loading...
Leave a Comment