Untitled

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

struct Solider {
    int id, score, team;
	Solider () {
		this->id = 0;
		this->score = 0;
		this -> team = 0;
	}
    Solider(int id, int score) {
        this->id = id;
        this->score = score;
    }
};

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(i, 0); 
    }
    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(Solider(mID, soliders[mID].score));
    if (it != team[t].end()) {
        team[t].erase(it);  // Erase the soldier from the team
    }

    soliders[mID].team = 0;  // Set soldier's team to 0
}

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(Solider(mID, soliders[mID].score));
    if (it != team[t].end()) {
        team[t].erase(it);  // Erase the old record
    }
    Solider newMember = Solider(mID, mScore);
    soliders[mID].score = mScore;  // Update soldier's score
    team[t].insert(newMember);  // Insert the updated record
}

void updateTeam(int mTeam, int mChangeScore) {
    for (auto it = team[mTeam].begin(); it != team[mTeam].end(); ++it) {
		int id = it->id;
        int newScore = it->score + mChangeScore;
        if (newScore > 5) newScore = 5;
        if (newScore < 1) newScore = 1;
		team[mTeam].erase(it);
		Solider newMember = Solider(id, newScore);
		team[mTeam].insert(newMember);
		soliders[id].score = newScore;
    }
}

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