Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
1.7 kB
2
Indexable
Never
#include <iostream>
#include <vector>
#include <set>

using namespace std;

struct Soldier {
    int id, score, team;
    
    Soldier() : id(0), score(0), team(0) {}
    Soldier(int id, int score, int team = 0) : id(id), score(score), team(team) {}
    
    bool operator<(const Soldier& other) const {
        if (score == other.score) {
            return id < other.id;
        }
        return score > other.score;
    }
};

vector<Soldier> soldiers(100001);
set<Soldier> team[6];  // Teams from 1 to 5

void init() {
    for (int i = 1; i <= 100000; i++) {
        soldiers[i] = Soldier(i, 0);
    }
    for (int i = 1; i <= 5; i++) {
        team[i].clear();
    }
}

void hire(int mID, int mTeam, int mScore) {
    fire(mID);  // Remove from previous team if any
    soldiers[mID] = Soldier(mID, mScore, mTeam);
    team[mTeam].insert(soldiers[mID]);
}

void fire(int mID) {
    int t = soldiers[mID].team;
    if (t == 0) return;
    team[t].erase(soldiers[mID]);
    soldiers[mID].team = 0;
    soldiers[mID].score = 0;
}

void updateSoldier(int mID, int mScore) {
    int t = soldiers[mID].team;
    if (t == 0) return;
    team[t].erase(soldiers[mID]);
    soldiers[mID].score = mScore;
    team[t].insert(soldiers[mID]);
}

void updateTeam(int mTeam, int mChangeScore) {
    set<Soldier> newTeam;
    for (const auto& soldier : team[mTeam]) {
        int newScore = max(1, min(5, soldier.score + mChangeScore));
        soldiers[soldier.id].score = newScore;
        newTeam.insert(Soldier(soldier.id, newScore, mTeam));
    }
    team[mTeam] = move(newTeam);
}

int bestSoldier(int mTeam) {
    if (team[mTeam].empty()) return 0;
    return team[mTeam].begin()->id;
}
Leave a Comment