Untitled

 avatar
Darin
plain_text
a year ago
2.6 kB
1
Indexable
Never
import java.util.*;
 
 
class UserSolution {
    TreeSet<Passenger> sections[];
    Passenger passengers[];
    List<Integer> jobs[];
 
 
    void init(int N, int M, int J, int mPoint[], int mJobID[]) {
        sections = new TreeSet[N / M];
        jobs = new ArrayList[J];
        for (int i = 0; i < J; i++)
            jobs[i] = new ArrayList<>();
        passengers = new Passenger[N];
        for (int i = 0; i < N; i++) {
            if (sections[i / M] == null)
                sections[i / M] = new TreeSet<>((a, b) -> a.point == b.point ? a.id - b.id : b.point - a.point);
            passengers[i] = new Passenger(i, mPoint[i], mJobID[i]);
            sections[i / M].add(passengers[i]);
            jobs[mJobID[i]].add(i);
        }
    }
 
 
    void destroy() {
    }
 
 
    int update(int mID, int mPoint) {
        Passenger p = passengers[mID];
        for (int i = 0; i < sections.length; i++) {
            if (sections[i].remove(p)) {
                p.point += mPoint;
                sections[i].add(p);
                break;
            }
        }
        return p.point;
    }
 
 
    int updateByJob(int mJobID, int mPoint) {
        int sum = 0;
        for (int i = 0; i < sections.length; i++) {
            for (int id : jobs[mJobID]) {
                if (sections[i].remove(passengers[id])) {
                    passengers[id].point += mPoint;
                    sum += passengers[id].point;
                    sections[i].add(passengers[id]);
                }
            }
        }
        return sum;
    }
 
 
    int move(int mNum) {
        int sum = 0;
        List<Passenger> temp[] = new ArrayList[sections.length - 1];
        for (int i = 0; i < sections.length - 1; i++) {
            temp[i] = new ArrayList<>();
            for (int j = 0; j < mNum; j++) {
                Passenger smallest = sections[i].pollLast();
                temp[i].add(smallest);
                sum += smallest.point;
            }
            for (int j = 0; j < mNum; j++) {
                Passenger largest = sections[i + 1].pollFirst();
                sections[i].add(largest);
                sum += largest.point;
            }
        }
        for (int i = 0; i < temp.length; i++)
            sections[i + 1].addAll(temp[i]);
        return sum;
    }
}
 
 
class Passenger {
    int id, point, jobID;
 
 
    Passenger(int mID, int mPoint, int mJobID) {
        id = mID;
        point = mPoint;
        jobID = mJobID;
    }
}