#include <iostream>
#define MAX_SOLDIERS 100001
#define MAX_SCORE 6
#define MAX_TEAM 6
using namespace std;
struct Soldier{
int mTeam;
int next, prev;
}Soldiers[MAX_SOLDIERS];
int head[MAX_TEAM][MAX_SCORE], tail[MAX_TEAM][MAX_SCORE];
void init()
{
for(int i = 1; i < MAX_TEAM; ++i){
for(int j = 1; j < MAX_SCORE; ++j){
head[i][j] = 0;
tail[i][j] = 0;
}
}
}
void hire(int mID, int mTeam, int mScore)
{
Soldiers[mID].mTeam = mTeam;
Soldiers[mID].prev = Soldiers[mID].next = 0;
if(head[mTeam][mScore] == 0){
head[mTeam][mScore] = tail[mTeam][mScore] = mID;
return;
}
Soldiers[tail[mTeam][mScore]].next = mID;
Soldiers[mID].prev = tail[mTeam][mScore];
tail[mTeam][mScore] = mID;
}
void fire(int mID)
{
int mTeam = Soldiers[mID].mTeam;
for(int score = 1; score < MAX_SCORE; ++score){
if(head[mTeam][score] == 0){
continue;
}
if(head[mTeam][score] == tail[mTeam][score] && head[mTeam][score] == mID){
head[mTeam][score] = tail[mTeam][score] = 0;
return;
}
if(head[mTeam][score] == mID){
head[mTeam][score] = Soldiers[mID].next;
Soldiers[Soldiers[mID].next].prev = 0;
return;
}
if(tail[mTeam][score] == mID){
tail[mTeam][score] = Soldiers[mID].prev;
Soldiers[Soldiers[mID].prev].next = 0;
return;
}
}
Soldiers[Soldiers[mID].prev].next = Soldiers[mID].next;
Soldiers[Soldiers[mID].next].prev = Soldiers[mID].prev;
}
void updateSoldier(int mID, int mScore)
{
int mTeam = Soldiers[mID].mTeam;
fire(mID);
hire(mID, mTeam, mScore);
}
void connect(const int& mTeam,const int& src,const int& dst){
if(src == dst){
return;
}
}
void updateTeam(int mTeam, int mChangeScore)
{
if(mChangeScore == 0){
return;
}
if(mChangeScore < 0){
for(int score = 1; score < MAX_SCORE; ++score){
int lastScore = score + mChangeScore;
if(lastScore < 1){
lastScore = 1;
}
connect(mTeam, score, lastScore);
}
}
}
int bestSoldier(int mTeam)
{
return 0;
}
int main(){
init();
hire(16, 1, 5);
hire(21, 1, 5);
//hire(20, 1, 5);
//cout << bestSoldier(1) << endl;
fire(21);
cout << bestSoldier(1) << endl;
hire(25, 1, 4);
hire(30, 1, 2);
updateTeam(1, 1);
cout << bestSoldier(1) << endl;
updateTeam(1, 2);
cout << bestSoldier(1) << endl;
updateSoldier(30, 2);
cout << bestSoldier(1) << endl;
updateTeam(1, -4);
hire(1, 1, 3);
updateTeam(1, -1);
cout << bestSoldier(1) << endl;
hire(100000, 5, 1);
cout << bestSoldier(5) << endl;
return 0;
}
//
//#include <iostream>
//#define MAX_SOLDIER 100001
//#define MAX_SCORE 6
//#define MAX_TEAM 6
//using namespace std;
//struct SOLDIER{
// int mID;
// int mTeam;
// SOLDIER* prev;
// SOLDIER* next;
// void set(int mID, int mTeam){
// this->mID = mID;
// this->mTeam = mTeam;
// prev = next = nullptr;
// }
//}*TABLESOLDIER[MAX_TEAM][MAX_SCORE], *TAIL[MAX_TEAM][MAX_SCORE], SOLDIERS[MAX_SOLDIER];
//
//void init()
//{
// for(int i = 1; i <= MAX_TEAM; ++i){
// for(int j = 1; j <= MAX_SCORE; ++j){
// TABLESOLDIER[i][j] = nullptr;
// TAIL[i][j] = nullptr;
// }
// }
//}
//
//void hire(int mID, int mTeam, int mScore)
//{
// SOLDIERS[mID].set(mID, mTeam);
// if(TAIL[mTeam][mScore] == nullptr){
// TABLESOLDIER[mTeam][mScore] = &SOLDIERS[mID];
// TAIL[mTeam][mScore] = &SOLDIERS[mID];
// return;
// }
// TAIL[mTeam][mScore]->next = &SOLDIERS[mID];
// SOLDIERS[mID].prev = TAIL[mTeam][mScore];
// TAIL[mTeam][mScore] = &SOLDIERS[mID];
//}
//void fire(int mID)
//{
// int mTeam = SOLDIERS[mID].mTeam;
// for(int i = 1; i < MAX_SCORE; ++i){
// if(TABLESOLDIER[mTeam][i] == nullptr){
// continue;
// }
// if(TABLESOLDIER[mTeam][i] == TAIL[mTeam][i] && TABLESOLDIER[mTeam][i]->mID == mID){
// TABLESOLDIER[mTeam][i] = TAIL[mTeam][i] = nullptr;
// return;
// }
// if(TABLESOLDIER[mTeam][i]->mID == mID){
// TABLESOLDIER[mTeam][i] = TABLESOLDIER[mTeam][i]->next;
// TABLESOLDIER[mTeam][i]->prev = nullptr;
// return;
// }
// else if(TAIL[mTeam][i]->mID == mID){
// TAIL[mTeam][i] = TAIL[mTeam][i]->prev;
// TAIL[mTeam][i]->next = nullptr;
// return;
// }
// }
// SOLDIERS[mID].prev->next = SOLDIERS[mID].next;
// SOLDIERS[mID].next->prev = SOLDIERS[mID].prev;
//}
//
//void updateSoldier(int mID, int mScore)
//{
// int team = SOLDIERS[mID].mTeam;
// fire(mID);
// hire(mID, team, mScore);
//}
//
//void connect(int mTeam, int src, int dst){
// if(src == dst || TABLESOLDIER[mTeam][src] == nullptr){
// return;
// }
// if(TABLESOLDIER[mTeam][dst] == nullptr){
// TABLESOLDIER[mTeam][dst] = TABLESOLDIER[mTeam][src];
// TAIL[mTeam][dst] = TAIL[mTeam][src];
// TABLESOLDIER[mTeam][src] = TAIL[mTeam][src] = nullptr;
// return;
// }
// TABLESOLDIER[mTeam][src]->prev = TAIL[mTeam][dst];
// TAIL[mTeam][dst]->next = TABLESOLDIER[mTeam][src];
// TAIL[mTeam][dst] = TAIL[mTeam][src];
// TABLESOLDIER[mTeam][src] = TAIL[mTeam][src] = nullptr;
//}
//
//void updateTeam(int mTeam, int mChangeScore)
//{
// if(mChangeScore == 0){
// return;
// }
// if(mChangeScore <= -1){
// for(int i = 1; i < MAX_SCORE; ++i){
// int lastScore = mChangeScore + i;
// if(lastScore < 1){
// lastScore = 1;
// }
// connect(mTeam, i, lastScore);
// }
// }
// else if(mChangeScore >= 1){
// for(int i = MAX_SCORE - 1; i >= 1; --i){
// int lastScore = mChangeScore + i;
// if(lastScore > 5){
// lastScore = 5;
// }
// connect(mTeam, i, lastScore);
// }
// }
//}
//
//int bestSoldier(int mTeam)
//{
// struct SOLDIER* tm;
// if(TABLESOLDIER[mTeam][5] != nullptr){
// tm = TABLESOLDIER[mTeam][5];
// }
// else if(TABLESOLDIER[mTeam][4] != nullptr){
// tm = TABLESOLDIER[mTeam][4];
// }
// else if(TABLESOLDIER[mTeam][3] != nullptr){
// tm = TABLESOLDIER[mTeam][3];
// }
// else if(TABLESOLDIER[mTeam][2] != nullptr){
// tm = TABLESOLDIER[mTeam][2];
// }
// else if(TABLESOLDIER[mTeam][1] != nullptr){
// tm = TABLESOLDIER[mTeam][1];
// }
// int maxID = 0;
// while (tm != nullptr)
// {
// if(maxID < tm->mID){
// maxID = tm->mID;
// }
// tm = tm->next;
// }
// return maxID;
//}
//int main(){
// init();
// hire(16, 1, 5);
// hire(21, 1, 5);
// cout << bestSoldier(1) << endl;
// fire(21);
// cout << bestSoldier(1) << endl;
// hire(25, 1, 4);
// hire(30, 1, 2);
// updateTeam(1, 1);
// cout << bestSoldier(1) << endl;
// updateTeam(1, 2);
// cout << bestSoldier(1) << endl;
// updateSoldier(30, 2);
// cout << bestSoldier(1) << endl;
// updateTeam(1, -4);
// hire(1, 1, 3);
// updateTeam(1, -1);
// cout << bestSoldier(1) << endl;
// hire(100000, 5, 1);
// cout << bestSoldier(5) << endl;
//
// return 0;
//}
///
#define MAX_SOL 100003
#define MAX_TEAM 6
#define MAX_SCORE 6
struct SOLDIER
{
int team;
int next, pre;
} Soldier[MAX_SOL];
int HeadTeam[MAX_TEAM][MAX_SCORE];
int TailTeam[MAX_TEAM][MAX_SCORE];
void init()
{
for (int i = 1; i < MAX_TEAM; i++ )
{
for (int j = 1; j < MAX_SCORE; j++)
{
HeadTeam[i][j] = 0;
TailTeam[i][j] = 0;
}
}
}
void hire(int mID, int mTeam, int mScore)
{
Soldier[mID].team = mTeam;
Soldier[mID].next = 0, Soldier[mID].pre = 0;
if (TailTeam[mTeam][mScore] == 0) // lisst rong-> them vao dau
{
HeadTeam[mTeam][mScore] = mID;
TailTeam[mTeam][mScore] = mID;
}
else // them Soldier moi vao duoi cua list co diem la mScore
{
int lastID = TailTeam[mTeam][mScore];
Soldier[lastID].next = mID;
Soldier[mID].pre = lastID;
TailTeam[mTeam][mScore] = mID;
}
}
void fire(int mID)
{
int mTeam = Soldier[mID].team;
int pre = Soldier[mID].pre;
int next = Soldier[mID].next;
bool isMid = true;
for (int score = 1; score < MAX_SCORE; score++)
{
if (HeadTeam[mTeam][score] == mID)
{
isMid = false;
HeadTeam[mTeam][score] = next;;
Soldier[next].pre = 0;
}
if (TailTeam[mTeam][score] == mID)
{
isMid = false;
TailTeam[mTeam][score] = pre;
Soldier[pre].next = 0;
}
}
if (isMid == true)
{
Soldier[pre].next = next;
Soldier[next].pre = pre;
}
}
void updateSoldier(int mID, int mScore)
{
int mTeam = Soldier[mID].team;
fire(mID);
hire (mID,mTeam, mScore);
}
void move (int mTeam, int src, int dst)
{
if (TailTeam[mTeam][src] == 0) return;
if (TailTeam[mTeam][dst] == 0)
{
HeadTeam[mTeam][dst] = HeadTeam[mTeam][src];
TailTeam[mTeam][dst] = TailTeam[mTeam][src];
}
else
{
int idTailDst = TailTeam[mTeam][dst];
int idHeadSrc = HeadTeam[mTeam][src];
Soldier[idTailDst].next = idHeadSrc;
Soldier[idHeadSrc].pre = idTailDst;
TailTeam[mTeam][dst] = TailTeam[mTeam][src];
}
HeadTeam[mTeam][src] = TailTeam[mTeam][src] = 0;
}
void updateTeam(int mTeam, int mChangeScore)
{
if (mChangeScore > 0 )
{
for (int i = 5; i >= 1; i--)
{
int newScore = i + mChangeScore ;
if (newScore > 5)
{
newScore = 5;
}
if (i != newScore)
{
move(mTeam, i, newScore);
}
}
}
else
{
for (int i = 1; i <= 5; i++)
{
int newScore = i + mChangeScore ;
if (newScore < 1)
{
newScore = 1;
}
if (i != newScore)
{
move(mTeam, i, newScore);
}
}
}
}
int bestSoldier(int mTeam)
{
int bestID = 0;
for (int i = 5; i >= 1; i--)
{
bestID = HeadTeam[mTeam][i];
for (int id = HeadTeam[mTeam][i]; id; id = Soldier[id].next)
{
if (id > bestID)
{
bestID = id;
}
}
if (bestID > 0) break;
}
return bestID;
}