Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.1 kB
2
Indexable
Never
#include <list>
using namespace std;
#define MAX_SOLDIERS 200002
#define MAX_TEAMS 6
#define MAX_SCORE 6
 
struct soldier {
    int Id;
    int team;
    bool isFired;
    void init(int Id, int team, bool isFired) {
        this->Id = Id;   // this đươc dùng trỏ cụ thể trong struct hoặc class
        this->team = team;
        this->isFired = isFired;
    }
} Soldiers[MAX_SOLDIERS];

int index;
list <int> teams[MAX_TEAMS][MAX_SCORE];
int Id_to_Index[MAX_SOLDIERS];
 
void init()
{
    index = 0;
    for (int i = 0; i < MAX_TEAMS; i++) {
        for (int j = 0; j < MAX_SCORE; j++) {
            teams[i][j].clear();
        }
    }
}
 
void hire(int mID, int mTeam, int mScore)
{
    index++;
    Soldiers[index].init(mID, mTeam, false);
    teams[mTeam][mScore].push_back(index);
    Id_to_Index[mID] = index;
}

 
void fire(int mID)
{
    Soldiers[Id_to_Index[mID]].isFired = true;
	
}
 
void updateSoldier(int mID, int mScore)
{
    int team = Soldiers[Id_to_Index[mID]].team;
	
    fire(mID);
    hire(mID, team, mScore);
}
 
void updateScore(int mTeam, int mScore, int mChangeScore)
{
    int newScore = mScore + mChangeScore;
    if (newScore <= 1)  newScore = 1;
    if (newScore >= 5)  newScore = 5;
    teams[mTeam][newScore].splice(teams[mTeam][newScore].end(), teams[mTeam][mScore]);
}
 
void updateTeam(int mTeam, int mChangeScore)
{
   if (mChangeScore == 0)  return;
   if (mChangeScore > 0) {
        for (int i = 4; i >= 0; i--) {
            updateScore(mTeam, i, mChangeScore);
        }
    }
    else {
        for (int i = 1; i <= 5; i++) {
            updateScore(mTeam, i, mChangeScore);
        }
    }
}
 
int bestSoldier(int mTeam)
{
    int res = -1;
    for (int i = 5; i >= 0; i--) {
        for (int it : teams[mTeam][i]) {
            if (Soldiers[it].isFired == false && Soldiers[it].Id > res) {
                res = Soldiers[it].Id;
            }
        }
        if (res != -1) return res;
   }
    return res;
}
Leave a Comment