Untitled

 avatar
unknown
plain_text
5 months ago
3.2 kB
2
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;
    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) {
    int mCategory = global_all[mGoodsID];
    if (global[mCategory].find(mGoodsID) != global[mCategory].end()) {
        global[mCategory][mGoodsID].deleted = true;
        // Remove from prod_data when marking as deleted
        prod_data[mCategory].erase(make_pair(global[mCategory][mGoodsID].rank, mGoodsID));
    }
}

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)); // Remove old rank
        prod_data[mCategory].emplace(prod.rank, mGoodsID); // Add with new rank
    }
}

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;
            newScore = min(max(newScore, 0), 100); // Ensure score is within bounds
            updateRank(it.first, newScore);
        }
    }
}

int getTopRank(int mCategory) {
    if (mCategory == 0) {
        pair<int, int> ans = {5, 0}; // Initialize with the worst rank and no GoodsID
        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 = it;
                    }
                }
            }
        }
        return ans.second;
    } else {
        for (const auto& it : prod_data[mCategory]) {
            if (!global[mCategory][it.second].deleted) {
                return it.second;
            }
        }
    }
    return 0; // If no valid product found
}
Editor is loading...
Leave a Comment