Untitled

 avatar
Darin
plain_text
a year ago
4.1 kB
3
Indexable
Never
import java.util.*;

class People {
    public int ID;
    public int jobID;
    public int point;
    public int section;
    public boolean status;

    public void init(int ID1, int jobID1, int point1, int section1) {
        ID = ID1;
        jobID = jobID1;
        point = point1;
        section = section1;
        status = true;
    }
}

People[] pool = new People[180000];
int poolCnt;

People getPeople(int ID1, int jobID1, int point1, int section1) {
    pool[poolCnt].init(ID1, jobID1, point1, section1);
    return pool[poolCnt++];
}

People getPeople(People p) {
    return getPeople(p.ID, p.jobID, p.point, p.section);
}

class CmpMaxHeap implements Comparator<People> {
    public int compare(People p1, People p2) {
        if (p1.point != p2.point) return p2.point - p1.point;
        return p2.ID - p1.ID;
    }
}

class CmpMinHeap implements Comparator<People> {
    public int compare(People p1, People p2) {
        if (p1.point != p2.point) return p2.point - p1.point;
        return p2.ID - p1.ID;
    }
}

class Section {
    public PriorityQueue<People> maxHeap = new PriorityQueue<>(new CmpMaxHeap());
    public PriorityQueue<People> minHeap = new PriorityQueue<>(new CmpMinHeap());
}

int secNumber;
Section[] listSec = new Section[11];
HashMap<Integer, People> hashPeople = new HashMap<>();
HashMap<Integer, ArrayList<People>> hashJob = new HashMap<>();

void addNewPeople(People p) {
    hashPeople.put(p.ID, p);
    hashJob.get(p.jobID).add(p);
    listSec[p.section].maxHeap.add(p);
    listSec[p.section].minHeap.add(p);
}

void init(int N, int M, int J, int[] mPoint, int[] mJobID) {
    poolCnt = 0;
    secNumber = N / M;
    hashPeople.clear();
    hashJob.clear();
    for (int i = 0; i < secNumber; i++) {
        listSec[i] = new Section();
        listSec[i].maxHeap.clear();
        listSec[i].minHeap.clear();
    }
    People p;
    for (int i = 0; i < N; i++) {
        p = getPeople(i, mJobID[i], mPoint[i], i / M);
        if (!hashJob.containsKey(mJobID[i])) {
            hashJob.put(mJobID[i], new ArrayList<>());
        }
        addNewPeople(p);
    }
}

void destroy() {}

int update(int mID, int mPoint) {
    People oldPeople = hashPeople.get(mID);
    oldPeople.status = false;
    People newPeople = getPeople(oldPeople);
    newPeople.point += mPoint;
    addNewPeople(newPeople);
    return newPeople.point;
}

int updateByJob(int mJobID, int mPoint) {
    int sum = 0;
    ArrayList<People> pByJob = hashJob.get(mJobID);
    hashJob.put(mJobID, new ArrayList<>());
    People newPeople;
    for (People tp : pByJob) {
        if (!tp.status) continue;
        tp.status = false;
        newPeople = getPeople(tp);
        newPeople.point += mPoint;
        addNewPeople(newPeople);
        sum += newPeople.point;
    }
    return sum;
}
int move(int mNum) {
    int sum = 0;
    ArrayList<People>[] moveUp = new ArrayList[10];
    ArrayList<People>[] moveDown = new ArrayList[10];
    int cnt;
    People tp, newPeople;
    for (int i = 0; i < secNumber - 1; i++) {
        // moveDown mNum People from i to i + 1
        // moveUp mNum People from i + 1 to i
        cnt = 0;
        while (cnt < mNum) {
            tp = listSec[i].minHeap.peek();
            listSec[i].minHeap.poll();
            if (!tp.status) continue;
            tp.status = false;
            cnt++;
            sum += tp.point;

            newPeople = getPeople(tp);
            moveDown[i].add(newPeople);
        }
        cnt = 0;
        while (cnt < mNum) {
            tp = listSec[i + 1].maxHeap.peek();
            listSec[i + 1].maxHeap.poll();
            if (!tp.status) continue;
            tp.status = false;
            cnt++;
            sum += tp.point;

            newPeople = getPeople(tp);
            moveUp[i + 1].add(newPeople);
        }
    }
    for (int i = 0; i < secNumber - 1; i++) {
        for (People p : moveDown[i]) {
            p.section = i + 1;
            addNewPeople(p);
        }
        for (People p : moveUp[i + 1]) {
            p.section = i;
            addNewPeople(p);
        }
    }
    return sum;
}