Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.0 kB
2
Indexable
Never
package SoldierManagement;

import java.net.ConnectException;
import java.util.LinkedList;
import java.util.List;

class UserSolution {
	class Soldier {
		public int ID;
		public int team;
		Soldier next, prev;

		public Soldier(int iD, int team) {
			this.ID = iD;
			this.team = team;
		}

		void delete() {
			connect(prev, next);
		}

	}

	void connect(Soldier a, Soldier b) {
		a.next = b;
		b.prev = a;
	}

	class DBLList {
		Soldier head, tail;

		public DBLList() {
			this.head = new Soldier(-1, -1);
			this.tail = new Soldier(-1, -1);
			connect(head, tail);
		}

		void reset() {
			connect(head, tail);
		}

		boolean isEmpty() {
			return head.next == tail;
		}

		void addLast(Soldier s) {
			connect(tail.prev, s);
			connect(s, tail);
		}

		void addLL(DBLList ll) {
			if (ll.isEmpty()) {
				return;
			}
			Soldier t1 = tail.prev;
			Soldier h2 = ll.head.next;
			Soldier t2 = ll.tail.prev;
			connect(t1, h2);
			connect(t2, tail);
			ll.reset();
		}

	}

	class Team {
		DBLList list[];

		Team() {
			list = new DBLList[6];
			for (int i = 0; i < 6; i++) {
				list[i] = new DBLList();
			}
		}

		void hire(Soldier s, int score) {
			list[score].addLast(s);
		}

		void updateScore(Soldier s, int newScore) {
			s.delete();
			list[newScore].addLast(s);
		}

		void changeScore(int oldScore, int newScore) {
			if (list[oldScore].isEmpty()) {
				return;
			}
			if (newScore < 1) {
				newScore = 1;
			}
			if (newScore > 5) {
				newScore = 5;
			}
			if (oldScore == newScore) {
				return;
			}
			list[newScore].addLL(list[oldScore]);
		}

		void updateTeam(int mChange) {
			if (mChange > 0) {
				for (int i = 5; i >= 1; i--) {
					changeScore(i, i + mChange);
				}
			} else {
				for (int i = 1; i <= 5; i++) {
					changeScore(i, i + mChange);
				}
			}
		}

		int bestSoldier() {
			for (int i = 5; i >= 1; i--) {
				if (list[i].isEmpty()) {
					continue;
				}
				int max = -1;
				Soldier sold = list[i].head.next;
				while (sold != list[i].tail) {
					if (max < sold.ID) {
						max = sold.ID;
					}
					sold = sold.next;
				}
				return max;
			}
			return -1;
		}
	}

	Soldier soldier[];
	Team teamarr[];

	public void init() {
		soldier = new Soldier[100002];
		teamarr = new Team[6];
		for (int i = 1; i < 6; i++) {
			teamarr[i] = new Team();
		}
	}

	public void hire(int mID, int mTeam, int mScore) {
		Soldier s = new Soldier(mID, mTeam);
		teamarr[mTeam].hire(s, mScore);
		soldier[mID] = s;

	}

	public void fire(int mID) {
		soldier[mID].delete();
	}

	public void updateSoldier(int mID, int mScore) {
		teamarr[soldier[mID].team].updateScore(soldier[mID], mScore);
	}

	public void updateTeam(int mTeam, int mChangeScore) {
		teamarr[mTeam].updateTeam(mChangeScore);
	}

	public int bestSoldier(int mTeam) {

		return teamarr[mTeam].bestSoldier();
	}

}