Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.8 kB
2
Indexable
Never
#include <iostream>
#include <vector>
#include <set>
using namespace std;

struct Solider {
    int id, score, team;
    Solider(int id = 0, int score = 0) : id(id), score(score) {}

    // To use 'find' in set, we need to compare only by id.
    bool operator<(const Solider& other) const {
        return id < other.id;
    }
};

Solider soliders[100001];

struct cmp {
    bool operator()(const Solider &a, const Solider &b) const {
        if (a.score == b.score) {
            return a.id < b.id;  // Sorting by id in ascending order if score is equal
        }
        return a.score > b.score;  // Sorting by score in descending order
    }
};

set<Solider, cmp> team[6];  // Teams from 1 to 5

void init() {
    for (int i = 1; i <= 100000; i++) {
        soliders[i] = Solider();  // Reset all soldiers
    }
    for (int i = 1; i <= 5; i++) {
        team[i].clear();  // Clear all teams
    }
}

void hire(int mID, int mTeam, int mScore) {
    soliders[mID] = Solider(mID, mScore);
    soliders[mID].team = mTeam;
    team[mTeam].insert(soliders[mID]);  // Insert soldier into the team
}

void fire(int mID) {
    int t = soliders[mID].team;
    if (t == 0) return;  // Soldier is not in any team

    auto it = team[t].find(soliders[mID]);
    if (it != team[t].end()) {
        team[t].erase(it);  // Erase the soldier from the team
    }

    soliders[mID] = Solider();  // Reset the soldier
}

void updateSoldier(int mID, int mScore) {
    int t = soliders[mID].team;
    if (t == 0) return;  // Soldier is not in any team

    auto it = team[t].find(soliders[mID]);
    if (it != team[t].end()) {
        team[t].erase(it);  // Erase the old record
    }
    
    soliders[mID].score = mScore;  // Update soldier's score
    team[t].insert(soliders[mID]);  // Insert the updated record
}

void updateTeam(int mTeam, int mChangeScore) {
    vector<Solider> updatedSoldiers;  // Temporary vector to store updated soldiers

    for (auto it = team[mTeam].begin(); it != team[mTeam].end(); ++it) {
        int newScore = it->score + mChangeScore;
        if (newScore > 5) newScore = 5;
        if (newScore < 1) newScore = 1;

        // Update soldier score and push to temporary vector
        Solider updatedSolider = *it;
        updatedSolider.score = newScore;
        updatedSoldiers.push_back(updatedSolider);

        // Update the global soldier record
        soliders[updatedSolider.id].score = newScore;
    }

    team[mTeam].clear();  // Clear the team set
    for (const auto& solider : updatedSoldiers) {
        team[mTeam].insert(solider);  // Reinsert updated soldiers
    }
}

int bestSoldier(int mTeam) {
    if (team[mTeam].empty()) return -1;  // No soldier in the team

    return team[mTeam].begin()->id;  // The first soldier has the highest score
}
Leave a Comment