Untitled

 avatar
Darin
plain_text
a year ago
3.0 kB
1
Indexable
Never
package SurvivalTrain;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;

class UserSolution {

	ArrayList<PriorityQueue<Passenger>> arrQueue;
	HashMap<Integer, Passenger> mapP;
	HashMap<Integer, ArrayList<Integer>> jobType;

	int numP, numS, numJ, numPS;

	void init(int N, int M, int J, int mPoint[], int mJobID[]) {
		numP = N;
		numS = N / M;
		numPS = M;
		numJ = J;
		mapP = new HashMap<>();
		jobType = new HashMap<>();
		arrQueue = new ArrayList<>();
		PriorityQueue<Passenger> PQ;

		for (int i = 0; i < N; i++) {
			Passenger P = new Passenger(i, mJobID[i], mPoint[i]);
			mapP.put(i, P);
			jobType.computeIfAbsent(mJobID[i], k -> new ArrayList<>()).add(i);
		}

		for (int i = 0; i < numS; i++) {
			PQ = new PriorityQueue<>((a, b) -> a.point == b.point ? b.id - a.id
					: a.point - b.point);
			for (int j = i * M; j < (i + 1) * M; j++) {
				PQ.add(mapP.get(j));
			}
			arrQueue.add(PQ);
		}
	}

	void destroy() {
	}

	int update(int mID, int mPoint) {
		if (mapP.containsKey(mID)) {
			mapP.get(mID).point += mPoint;
		}
		return mapP.get(mID).point;
	}

	int updateByJob(int mJobID, int mPoint) {
		int sum = 0;
		if (jobType.containsKey(mJobID)) {
			for (int i : jobType.get(mJobID)) {
				mapP.get(i).point += mPoint;
				sum += mapP.get(i).point;
			}
		}
		return sum;
	}

	int move(int mNum) {
		int sum = 0;
		PriorityQueue<Passenger> PQ;
		Passenger[][] Pmax = new Passenger[numS - 1][mNum];
		Passenger[][] Pmin = new Passenger[numS - 1][mNum];
		for (int i = 0; i < numS - 1; i++) {
			PQ = new PriorityQueue<Passenger>(new minFirst());
			PQ.addAll(arrQueue.get(i));
			arrQueue.set(i, PQ);
		}
		for (int i = 0; i < numS - 1; i++) {
			for (int k = 0; k < mNum; k++) {
				Pmin[i][k] = arrQueue.get(i).poll();
			}
		}
		for (int i = 1; i < numS; i++) {
			PQ = new PriorityQueue<Passenger>(new maxFirst());
			PQ.addAll(arrQueue.get(i));
			arrQueue.set(i, PQ);
		}
		for (int i = 0; i < numS - 1; i++) {
			for (int k = 0; k < mNum; k++) {
				Pmax[i][k] = arrQueue.get(i + 1).poll();
			}
		}
		for (int i = 0; i <= numS - 2; i++) {
			for (int k = 0; k < mNum; k++) {
				arrQueue.get(i).add(Pmax[i][k]);
				arrQueue.get(i + 1).add(Pmin[i][k]);
				sum += Pmin[i][k].point + Pmax[i][k].point;
			}
		}
		return sum;
	}

	class minFirst implements Comparator<Passenger> {
		public int compare(Passenger a, Passenger b) {
			if (a.point == b.point)
				return b.id - a.id;
			else
				return a.point - b.point;
		}

	}

	class maxFirst implements Comparator<Passenger> {
		public int compare(Passenger a, Passenger b) {
			if (a.point == b.point)
				return a.id - b.id;
			else
				return b.point - a.point;
		}
	}

	class Passenger {
		int id;
		int jId;
		int point;

		public Passenger(int id, int jId, int point) {
			this.id = id;
			this.jId = jId;
			this.point = point;
		}
	}
}