Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.5 kB
6
Indexable
Never
#define MAX 20011
#include <set>
using namespace std;
struct Compare {
    bool operator() (int idp1, int idp2);
};
struct STUDENT {
    int id, grade, nextH;
    int gender; // 0: female, 1: male
    int score;
    set<int, Compare>::iterator it;
} pool[MAX];
int Npool;
int hashTB[MAX];
bool Compare::operator() (int idp1, int idp2) {
    if (pool[idp1].score != pool[idp2].score)
        return pool[idp1].score > pool[idp2].score;
    return pool[idp1].id > pool[idp2].id;
}
set<int, Compare> mySet[4][2];
int getIndex(int mid){
    int key = mid % MAX;
    for (int id = hashTB[key]; id; id = pool[id].nextH){
        if (mid == pool[id].id)
            return id;
    }
    return 0;
}
void init() {
    for (int i = 0; i < MAX; i++)
        hashTB[i] = 0;
    Npool = 1;
    for (int i = 1; i < 4; i++)
    for (int j = 0; j < 2; j++)
        mySet[i][j].clear();
    return;
}
int add(int id, int grade, char gender[7], int score) {
    int mG = gender[0] == 'm' ? 1 : 0;
    int key = id % MAX;
    pool[Npool].nextH = hashTB[key];
    hashTB[key] = Npool;
    pool[Npool].gender = mG;
    pool[Npool].grade = grade;
    pool[Npool].id = id;
    pool[Npool].score = score;
    pool[Npool].it = mySet[grade][mG].insert(Npool).first;
    Npool++;
    int x = pool[*mySet[grade][mG].begin()].id;
    return x;
}
 
int remove(int mid) {
    int res = 0;
    int id = getIndex(mid);
    if (!id)
        return 0;
    mySet[pool[id].grade][pool[id].gender].erase(pool[id].it);
    if (mySet[pool[id].grade][pool[id].gender].size())
        res = pool[*(--mySet[pool[id].grade][pool[id].gender].end())].id;
    return res;
}
int query(int mGradeCnt, int mGrade[], int mGenderCnt, char mGender[][7], int mScore) {
    pool[0].score = mScore;
    int minScore = 400000;
    int minID = 1000000001;
    for (int i = 0; i < mGradeCnt; i++) {
        for (int j = 0; j < mGenderCnt; j++) {
            int mG = mGender[j][0] == 'm' ? 1 : 0;
            auto it = mySet[mGrade[i]][mG].lower_bound(0);
            if (it != mySet[mGrade[i]][mG].begin()) {
                it--;
                if ((pool[*it].score < minScore) || ((pool[*it].id < minID) && (pool[*it].score == minScore))) {
                    minScore = pool[*it].score;
                    minID = pool[*it].id;
                }
            }
        }
    }
    if (minID != 1000000001)
        return minID;
    return 0;
}