Untitled
unknown
plain_text
a year ago
3.0 kB
6
Indexable
import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedList; import java.util.TreeSet; class UserSolution { class P { int mID, mJID, mPoint; int sec; public P(int mID, int mJID, int mPoint, int sec) { this.mID = mID; this.mJID = mJID; this.mPoint = mPoint; this.sec = sec; } public P() { } } int k; HashMap<Integer, LinkedList<P>> hashMap = new HashMap<Integer, LinkedList<P>>(); @SuppressWarnings("unchecked") TreeSet<P>[] sections = new TreeSet[10]; HashMap<Integer, P> pHashMap = new HashMap<Integer, UserSolution.P>(); void init(int N, int M, int J, int mPoint[], int mJobID[]) { hashMap.clear(); pHashMap.clear(); k = N / M; for (int i = 0; i < k; i++) { sections[i] = new TreeSet<P>(new Comparator<P>() { @Override public int compare(P p1, P p2) { if (p1.mPoint == p2.mPoint) { return p1.mID - p2.mID; } return p2.mPoint - p1.mPoint; } }); for (int k = i * M; k < (i + 1) * M; k++) { P p = new P(k, mJobID[k], mPoint[k], i); sections[i].add(p); pHashMap.put(p.mID, p); hashMap.computeIfAbsent(p.mJID, value -> new LinkedList<P>()).add(p); } } } void destroy() { return; } int update(int mID, int mPoint) { P p = pHashMap.get(mID); sections[p.sec].remove(p); p.mPoint += mPoint; sections[p.sec].add(p); return p.mPoint; } int updateByJob(int mJobID, int mPoint) { int sum = 0; LinkedList<P> list = hashMap.get(mJobID); for (P p : list) { sections[p.sec].remove(p); p.mPoint += mPoint; sections[p.sec].add(p); sum += p.mPoint; } return sum; } int move(int mNum) { int sum = 0; LinkedList<P> f = new LinkedList<P>(); LinkedList<P> b = new LinkedList<P>(); for (int i = 0; i < k - 1; i++) { for (int j = 0; j < mNum; j++) { P p1 = sections[i].pollLast(); P p2 = sections[i + 1].pollFirst(); sum += (p1.mPoint + p2.mPoint); f.add(p1); b.add(p2); } } for (int i = 0; i < k - 1; i++) { for (int j = i * mNum; j < (i + 1) * mNum; j++) { P p1 = b.get(j); p1.sec = i; P p2 = f.get(j); p2.sec = i + 1; sections[i].add(p1); sections[i + 1].add(p2); } } return sum; } }
Editor is loading...
Leave a Comment