Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.9 kB
2
Indexable
Never
class UserSolution {
    LinkedList list[][];
    Soldier soldier[];
  
  
    public void init() {
        list = new LinkedList[6][6];
        soldier = new Soldier[100001];
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 6; j++) {
                list[i][j] = new LinkedList();
            }
        }
    }
  
    public void hire(int mID, int mTeam, int mScore) {
        Soldier ob = new Soldier(mID, mTeam);
        soldier[mID] = ob;
        ob.next = list[mTeam][mScore].tail;
        ob.pre = list[mTeam][mScore].tail.pre;
  
        list[mTeam][mScore].tail.pre = ob;
        ob.pre.next = ob;
    }
  
    public void fire(int mID) {
        Soldier ob = soldier[mID];
        link(ob.pre, ob.next);
    }
  
    public void updateSoldier(int mID, int mScore) {
        fire(mID);
        hire(mID, soldier[mID].Team, mScore);
    }
  
    public void updateTeam(int mTeam, int mChangeScore) {
        if (mChangeScore > 0) {
            for (int i = 4; i >= 1; i--) {
                int newScore = i + mChangeScore;
                if (newScore > 5) {
                    newScore = 5;
                }
                link(list[mTeam][newScore].tail.pre, list[mTeam][i].head.next);
                link(list[mTeam][i].tail.pre, list[mTeam][newScore].tail);
                link(list[mTeam][i].head, list[mTeam][i].tail);
            }
        } else if (mChangeScore < 0) {
            for (int i = 2; i <= 5; i++) {
                int newScore = i + mChangeScore;
                if (newScore < 1) {
                    newScore = 1;
                }
                link(list[mTeam][newScore].tail.pre, list[mTeam][i].head.next);
                link(list[mTeam][i].tail.pre, list[mTeam][newScore].tail);
                link(list[mTeam][i].head, list[mTeam][i].tail);
            }
        }
    }
  
    public int bestSoldier(int mTeam) {
        Soldier temp;
        int ans = 0;
        for (int i = 5; i >= 1; i--) {
            temp = list[mTeam][i].head.next;
            if (temp == list[mTeam][i].tail) {
                continue;
            }
            while (temp != null) {
                if (temp.ID > ans) {
                    ans = temp.ID;
                }
                temp = temp.next;
            }
            return ans;
        }
        return 0;
    }
  
    class Soldier {
        int ID, Team;
        Soldier pre, next;
  
        Soldier(int id, int team) {
            ID = id;
            Team = team;
            pre = next = null;
        }
    }
  
    class LinkedList {
        Soldier head, tail;
  
        LinkedList() {
            head = new Soldier(-1, -1);
            tail = new Soldier(-1, -1);
            link(head, tail);
        }
    }
  
    void link(Soldier pre, Soldier next) {
        pre.next = next;
        next.pre = pre;
    }
  
}
Leave a Comment