TA2220_Set

 avatar
Kotori
plain_text
a month ago
2.1 kB
3
Indexable
Never
#include<iostream>
#include<string>
#include<map>
#include<set>

using namespace std;

class Student {
public:
	int id, grade, gender, score;

	Student()
	{
		id = gender = grade = score = -1;
	}

	Student(int i, int gr, int ge, int s)
	{
		id = i;
		grade = gr;
		gender = ge;
		score = s;
	}


} S[200000];

void del(int index)
{
	S[index].id = S[index].gender = S[index].grade = S[index].score = -1;
}

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 indx = 0;

map<int, int> HashID;
set<int, cmp> SetS[4][3];		//S[grade][gender]

void init()
{
	cnt = 0;
	HashID.clear();
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			SetS[i][j].clear();
		}
	}

}

int add(int mId, int mGrade, char mGender[7], int mScore)
{
	indx = cnt++;
	int gender = (strcmp(mGender, "male") == 0) ? 0 : 1;
	S[indx] = { mId, mGrade, gender, mScore };
	HashID[mId] = indx;
	SetS[mGrade][gender].insert(indx);

	return 0;
}


int remove(int mId)
{
	auto a = HashID.find(mId);
	if (a == HashID.end())	return 0;

	int index = a->second;
	SetS[S[index].grade][S[index].gender].erase(index);

	int ans = 0;
	HashID.erase(a->first);
	if (SetS[S[index].grade][S[index].gender].size() == 0)
	{
		return 0;
	}
	else
	{
		ans = *SetS[S[index].grade][S[index].gender].begin();
	}
	del(index);
	ans = S[ans].id;
	return ans;
}

int query(int mGradeCnt, int mGrade[], int mGenderCnt, char mGender[][7], int mScore)
{
	int res = 1000000005;

	for (int i = 0; i < mGenderCnt; i++)
	{
		int gr = mGrade[i];
		for (int j = 0; j < mGenderCnt; j++)
		{
			int ge = (strcmp(mGender[j], "male") == 0) ? 0 : 1;
			for (auto it : SetS[gr][ge])
			{
				if (S[it].score >= mScore)
				{
					if (res > S[it].id)
					{
						res = S[it].id;
					}
					break;
				}
			}
		}
	}
	if (res == 1000000005)	
		return 0;
	return res;
}
Leave a Comment