Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.1 kB
3
Indexable
Never
import java.util.*;
 
class Student {
    int id;
    int score;
    int grade;
    int gender; // 0-male 1-female 
    Student (int id, int grade, char gender, int score){
        this.id = id;
        this.score = score;
        this.grade = grade;
        this.gender = (gender=='m') ? 0 : 1;
    }
}
 
class CmpStudent implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        if (o1.score == o2.score) 
            return o1.id - o2.id;
        return o1.score - o2.score;
    }
}
 
class UserSolution {
    private TreeSet<Student>[][] ts; 
    private HashMap<Integer, Student> records;
     
    public void init() {
        records = new HashMap<Integer, Student>();
        ts = new TreeSet[4][2];
        for (int i = 1; i <= 3; i++){
            for (int j = 0; j <= 1; j++){
                ts[i][j] = new TreeSet<Student>(new CmpStudent());
            }
        }
        return;
    }
 
    public int add(int mId, int mGrade, char mGender[], int mScore) {
        Student p = new Student(mId, mGrade, mGender[0], mScore);
        records.put(mId, p);
        ts[p.grade][p.gender].add(p);
        return ts[p.grade][p.gender].last().id;
    }
 
    public int remove(int mId) {
        Student sID = records.remove(mId);
        if (sID == null) return 0;
        ts[sID.grade][sID.gender].remove(sID);
        if (ts[sID.grade][sID.gender].isEmpty()) return 0;
        return ts[sID.grade][sID.gender].first().id;
    }
 
    public int query(int mGradeCnt, int mGrade[], int mGenderCnt, char mGender[][], int mScore) {
        TreeSet<Student> find = new TreeSet<Student>(new CmpStudent());
        for (int i = 0; i < mGradeCnt; i++){
            for (int j = 0; j < mGenderCnt; j++){
                Student p = new Student(-1, mGrade[i], mGender[j][0], mScore);
                Student cei = ts[p.grade][p.gender].ceiling(p);
                if (cei != null) find.add(cei);
            }
        }
        if (find.isEmpty()) return 0;
        return find.first().id;
    }
}