Untitled

 avatar
fttk
plain_text
a year ago
2.8 kB
1
Indexable
Never
Sodier manage
class UserSolution{
    class soldier{
        int id,team;
        soldier prev,nxt;
        soldier(int id,int team){
            this.id=id;this.team=team;
            prev=nxt=null;
        }
    }
    class linkedlist{
        soldier head,tail;
        linkedlist(){
            head=new soldier(-1,-1);
            tail=new soldier(-1,-1);
            head.nxt=tail;
            tail.prev=head;
        }
    }
    soldier[] sold;
    linkedlist[][] lst;
    public void init() {
        sold=new soldier[100001];
        lst=new linkedlist[6][6];
        for(int i=1;i<6;i++) {
            for(int j=1;j<6;j++) {
                lst[i][j]=new linkedlist();
            }
        }
    }
    public void hire(int mID, int mTeam, int mScore) {
        soldier ss=new soldier(mID,mTeam);
        sold[mID]=ss;
        ss.prev=lst[mTeam][mScore].tail.prev;
        ss.nxt=lst[mTeam][mScore].tail;
        ss.prev.nxt=ss;
        lst[mTeam][mScore].tail.prev=ss;
    }
    public void fire(int mID) {
        soldier ss=sold[mID];
        link(ss.prev,ss.nxt);
    }
    public void updateSoldier(int mID, int mScore) {
        fire(mID);
        hire(mID,sold[mID].team,mScore);
    }
    public void updateTeam(int mTeam, int mChangeScore) {
        if(mChangeScore>0) {
            for(int i=4;i>=1;i--) {
                int updatedscore=mChangeScore+i;
                if(updatedscore>5) {
                    updatedscore=5;
                }
                link(lst[mTeam][updatedscore].tail.prev,lst[mTeam][i].head.nxt);
                link(lst[mTeam][i].tail.prev,lst[mTeam][updatedscore].tail);
                link(lst[mTeam][i].head,lst[mTeam][i].tail);
            }
        }
        else if(mChangeScore<0) {
            for(int i=2;i<=5;i++) {
                int updatedscore=mChangeScore+i;
                if(updatedscore<1) {
                    updatedscore=1;
                }
                link(lst[mTeam][updatedscore].tail.prev,lst[mTeam][i].head.nxt);
                link(lst[mTeam][i].tail.prev,lst[mTeam][updatedscore].tail);
                link(lst[mTeam][i].head,lst[mTeam][i].tail);
            }
        }
    }
    public int bestSoldier(int mTeam) {
        soldier temp;
        int ans=0;
        for(int i=5;i>=1;i--) {
            temp=lst[mTeam][i].head.nxt;
            if(temp==lst[mTeam][i].tail) {
                continue;
            }
            while(temp!=lst[mTeam][i].tail) {
                if(temp.id>ans) {
                    ans=temp.id;
                }
                temp=temp.nxt;
            }
            return ans;
        }
        return 0;
    }
    public void link(soldier previous, soldier next) {
        previous.nxt=next;
        next.prev=previous;
    }
}