#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;
}
/////////
#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;
}