Untitled

 avatar
unknown
plain_text
a year ago
2.4 kB
10
Indexable
#include <iostream>
using namespace std ;

struct soldier
{
    int mid, mteam;
    soldier *next, *prev;
}s_arr[100002], head[6][6] = { 0 };
 
void init()
{
    for (int i = 1; i <= 5; i++)
    {
        for (int j = 1; j <= 5; j++)
        {
            head[i][j] = { 0,0, &head[i][j], &head[i][j] };
        }
    }
};
 
void hire(int mID, int mTeam, int mScore)
{
    s_arr[mID].mid = mID;
    s_arr[mID].mteam = mTeam;
    s_arr[mID].prev = &head[mTeam][mScore];
    s_arr[mID].next = head[mTeam][mScore].next;
    s_arr[mID].prev->next = s_arr[mID].next->prev = &s_arr[mID];
}
 
void fire(int mID)
{
    s_arr[mID].prev->next = s_arr[mID].next;
    s_arr[mID].next->prev = s_arr[mID].prev;
}
 
void updateSoldier(int mID, int mScore)
{
    fire(mID);
    hire(mID, s_arr[mID].mteam, mScore);
}
 
 
void moveList(int mTeam, int from, int to) {
    soldier *src_head = &head[mTeam][from];
    soldier *dst_head = &head[mTeam][to];
 
    if (src_head->next != src_head) {
        src_head->prev->next = dst_head->next;        // connecting tail of src to head of dst
        dst_head->next->prev = src_head->prev;      // connecting head of dst to tail of src
        dst_head->next = src_head->next;            // head of dst will be changed to first node of src
        dst_head->next->prev = dst_head;            // setting up previous pointer first node
        src_head->next = src_head->prev = src_head; // empty src as it has been moved to dst
    }
}
 
void updateTeam(int mTeam, int mChangeScore) {
    if (mChangeScore < 0) {
        for (int from = 2; from <= 5; from++) {
            int to = from + mChangeScore;
            to = (to > 1) ? to : 1;
            moveList(mTeam, from, to);
        }
    } else if (mChangeScore > 0) {
        for (int from = 4; from > 0; from--) {
            int to = from + mChangeScore;
            to = (to < 5) ? to : 5;
            moveList(mTeam, from, to);
        }
    }
}
 
 
 
 
int bestSoldier(int mTeam) {
    int bid = 0, score = 5;
    while (score > 0 && head[mTeam][score].next == &head[mTeam][score]) 
        score--;
 
    for (soldier *ptr = head[mTeam][score].next; ptr != &head[mTeam][score]; ptr = ptr->next) {
        if (ptr->mid > bid)
            bid = ptr->mid;
    }
    return bid;
}
Editor is loading...
Leave a Comment