Untitled
H2220_TA_CODEKotori
plain_text
a month ago
2.2 kB
0
Indexable
Never
#include<iostream> #include<string> #include<map> #include<set> #include<unordered_map> using namespace std; class Student { public: int id, grade, gender, score, isVailable; Student() { id = gender = grade = score = isVailable = -1; } Student(int i, int gr, int ge, int s) { id = i; grade = gr; gender = ge; score = s; } } S[200000]; struct cmp { bool operator() (const int a, const int b) const { if (S[a].score == S[b].score) return S[a].id < S[b].id; return S[a].score < S[b].score; } }; int cnt = 0; int index = 0; unordered_map<int, int> HashID; set<int, cmp> SetS[4][2]; //S[grade][gender] void init() { cnt = 1; HashID.clear(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 2; j++) { SetS[i][j].clear(); } } } int add(int mId, int mGrade, char mGender[7], int mScore) { index = cnt++; HashID[mId] = index; int ge = (strcmp(mGender, "male")) == 0 ? 0 : 1; S[index].id = mId; S[index].grade = mGrade; S[index].gender = ge; S[index].score = mScore; S[index].isVailable = 1; SetS[mGrade][ge].insert(index); auto it = --SetS[mGrade][ge].end(); int res = S[*it].id; return res; } int remove(int mId) { auto a = HashID.find(mId); if (a == HashID.end()) return 0; int cur_index = HashID[mId]; if (S[cur_index].isVailable == 0) return 0; S[cur_index].isVailable = 0; SetS[S[cur_index].grade][S[cur_index].gender].erase(cur_index); if (SetS[S[cur_index].grade][S[cur_index].gender].size() == 0) return 0; auto first = SetS[S[cur_index].grade][S[cur_index].gender].begin(); int res = S[*first].id; return res; } int query(int mGradeCnt, int mGrade[], int mGenderCnt, char mGender[][7], int mScore) { set<int, cmp> set_tmp; S[0].score = mScore; for (int i = 0; i < mGradeCnt; i++) { for (int j = 0; j < mGenderCnt; j++) { int ge = (strcmp(mGender[j], "male")) == 0 ? 0 : 1; auto a = SetS[mGrade[i]][ge].lower_bound(0); if (a == SetS[mGrade[i]][ge].end()) continue; set_tmp.insert(*a); } } if (set_tmp.size() == 0) return 0; auto a = set_tmp.begin(); int res = S[*a].id; return res; }
Leave a Comment