# Untitled

unknown

plain_text

20 days ago

14 kB

4

Indexable

Never

^{}

[Problem Description] There is a system managing school records. When adding a school record, the student’s grade, gender, and score are given with his/her ID. When deleting a school record, only the ID of the student is given. For school record inquiry, return the ID of the student with the lowest score among students with at least a certain score while meeting the conditions of grade (Grades 1 to 3) and gender (male/female). For instance, given the conditions of grade {Grade 1, Grade 3}, gender {female}, and score 80, among female students in Grades 1 and 3 whose scores are greater than or equal to 80, return the ID of the student with the lowest score. Given the conditions of grade {Grade 2}, gender {male, female}, and score 90, among male and female students in Grade 2 whose scores are greater than or equal to 90, return the ID of the student with the lowest score. You are required to implement a system managing school records that operates as mentioned above. Implement each required function by referring to the following API description. ※ The function signature below is based on C/C++. As for other languages, refer to the provided Main and User Code. The following is the description of API to be written in the User Code. void init() This function is called in the beginning of each test case. The system will have no registered school records. int add(int mId, int mGrade, char mGender[7], int mScore) This function adds mScore, the score of the student whose ID, grade, and gender are mId, mGrade, and mGender, respectively. In the case of gender, “male” is for men and “female” is for women. It is a string that ends with ‘＼0’. This function returns the ID of the student whose score is the highest among students in mGrade whose genders are mGender. If there are multiple students with the highest score, return the ID with the biggest value. The ID of the student whose score is already recorded in the system is not given as an input. However, it can be added again after the record is deleted. Parameters mId: The ID of student ( 1 ≤ mId ≤ 1,000,000,000 ) mGrade: The grade of student ( 1 ≤ mGrade ≤ 3 ) mGender: The gender of student ( A string of “male” or “female” that ends with ‘＼0’ ) mScore: The score of student ( 0 ≤ mScore ≤ 300,000 ) Returns Return the ID of the student with the highest score among students in mGrade whose genders are mGender. int remove(int mId) This function deletes the record of the student whose ID is mId. If the score of student mId is not recorded in the system, this function returns 0. After the deletion, this function returns the ID of the student whose score is the lowest among students of the same gender and in the same grade with student mId. If there are multiple students with the lowest score, return the ID with the smallest value. After the deletion, return 0 when there are NO students of the same gender in the same grade. Parameters mId: The ID of student ( 1 ≤ mId ≤ 1,000,000,000 ) Returns After the deletion, return the ID of the student whose score is the lowest among students of the same gender and in the same grade with student mId. If there is NO student mId, or if there are NO students of the same gender in the same grade with student mId, return 0. int query(int mGradeCnt, int mGrade[], int mGenderCnt, char mGender[][7], int mScore) This function returns the ID of the student with the lowest score among students within the sets of grade, mGrade, and gender, mGender, whose scores are greater than or equal to mScore. If there are multiple students with the lowest score, this function returns the ID with the smallest value. The number of grades, mGradeCnt, is given as mGrade array. For instance, Grades 1 and 3 are given as {1, 3}. The number of genders, mGenderCnt, is given as mGender array. For instance, men is given as {“male”} while men and women are given as {“male”, “female”}. If there is NO student whose score is greater than or equal to mScore, this function returns 0. Parameters mGradeCnt: The number of elements in mGrade array ( 1 ≤ mGradeCnt ≤ 3 ) mGrade: The array of grade ( 1 ≤ mGrade[i] ≤ 3 ) mGenderCnt: The number of elements in mGender array ( 1 ≤ mGenderCnt ≤ 2 ) mGender: The array of gender ( mGender[i] = A string of “male” or “female” that ends with ‘＼0’ ) mScore: The score of student ( 0 ≤ mScore ≤ 300,000 ) Returns Return the ID of the student with the lowest score among students within the sets of mGrade and mGender whose scores are greater than or equal to mScore. If there is NO student whose score is greater than or equal to mScore, return 0. [Example] Let’s look into a case where functions are called as in [Table 1] below.. Order Function return Figure 1 init() 2 add(500, 1, male, 1250) 500 3 add(400, 3, female, 2900) 400 4 add(900, 2, female, 2500) 900 5 add(700, 2, male, 2500) 700 6 add(600, 1, female, 1750) 600 7 add(800, 3, male, 1000) 800 Fig. 1 8 add(300, 2, female, 2000) 900 Fig. 2 9 query(2, {2, 3}, 2, {male, female}, 2500) 700 Fig. 3 10 query(3, {1, 2, 3}, 1, {male}, 1100) 500 Fig. 4 11 add(100, 2, female, 2500) 900 Fig. 5 12 query(1, {2}, 2, {male, female}, 2200) 100 Fig. 6 13 remove(300) 100 Fig. 7 14 remove(200) 0 15 add(300, 3, female, 3000) 300 Fig. 8 16 query(2, {1, 3}, 1, {female}, 1800) 400 Fig. 9 17 remove(800) 0 Fig. 10 18 query(3, {1, 2, 3}, 2, {male, female}, 1000) 500 Fig. 11 [Table 1] (Order 1) In the beginning, there are NO school records registered. (Order 2) Add 1250, the score of a male student in Grade 1 whose ID is 500. When it comes to male students in Grade 1, there is only the student whose ID is 500. Thus, return 500 for the student ID with the highest score. (Order 3) Add 2900, the score of a female student in Grade 3 whose ID is 400. When it comes to female students in Grade 3, there is only the student whose ID is 400. Thus, return 400 for the student ID with the highest score. (Order 4) Add 2500, the score of a female student in Grade 2 whose ID is 900. When it comes to female students in Grade 2, there is only the student whose ID is 900. Thus, return 900 for the student ID with the highest score. (Order 5) Add 2500, the score of a male student in Grade 2 whose ID is 700. When it comes to male students in Grade 2, there is only the student whose ID is 700. Thus, return 700 for the student ID with the highest score. (Order 6) Add 1750, the score of a female student in Grade 1 whose ID is 600. When it comes to female students in Grade 1, there is only the student whose ID is 600. Thus, return 600 for the student ID with the highest score. (Order 7) Add 1000, the score of a male student in Grade 3 whose ID is 800. When it comes to male students in Grade 3, there is only the student whose ID is 800. Thus, return 800 for the student ID with the highest score. [Fig. 1] shows the result of the function call. [Fig. 1] (Order 8) Add 2000, the score of a female student in Grade 2 whose ID is 300. Among female students in Grade 2, return 900 for the ID of the student with the highest score. [Fig. 2] shows the result of the function call. [Fig. 2] (Order 9) Students who are within the sets of {Grade 2, Grade 3} and {male, female} are students in the box colored yellow as in [Fig. 3]. Among them, students whose scores are greater than or equal to 2500 are colored blue. Among the students with blue font color, there are two students whose scores are the lowest, ID 700 and ID 900. Thus return 700, the ID whose value is smaller. [Fig. 3] (Order 10) Students who are within the sets of {Grade 1, Grade 2, Grade 3} and {male} are students in the box colored yellow as in [Fig. 4]. Among them, students whose scores are greater than or equal to 1100 are colored blue. Among the students with blue font color, return 500 for the ID of the student with the lowest score. [Fig. 4] (Order 11) Add 2500, the score of a female student in Grade 2 whose ID is 100. Among female students in Grade 2, there are two students with the highest score, ID 100 and ID 900. Thus, return 900, the ID whose value is bigger. [Fig. 5] shows the result of the function call. [Fig. 5] (Order 12) Students who are within the sets of {Grade 2} and {male, female} are students in the box colored yellow as in [Fig. 6]. Among them, students whose scores are greater than or equal to 2200 are colored blue. Among the students with blue font color, there are three students with the lowest score, ID 700, ID 100, and ID 900. Thus, return 100, the ID with the lowest value. [Fig. 6] (Order 13) Delete the record of the student whose ID is 300. After the deletion, among students who are of the same gender and in the same grade with the student whose ID is 300, there are two students with the lowest score, ID 100 and ID 900. Between the two, return 100, the ID whose value is smaller. [Fig. 7] shows the result of the function call. [Fig. 7] (Order 14) Delete the record of the student whose ID is 200. As there is NO record of a student whose ID is 200 in the system, the deletion fails. Thus, return 0. (Order 15) Add 3000, the score of a female student in Grade 3 whose ID is 300. Among female students in Grade 3, return 300, the ID of the student with the highest score. [Fig. 8] shows the result of the function call. [Fig. 8] (Order 16) Students who are within the sets of {Grade 1, Grade 3} and {female} are students in the box colored yellow as in [Fig. 9]. Among them, students whose scores are greater than or equal to 1800 are colored blue. Among the students with blue font color, return 400 for the ID of the student with the lowest score. [Fig. 9] (Order 17) Delete the record of the student whose ID is 800. After the deletion, as there are NO students who are of the same gender and in the same grade with the student whose ID is 800, return 0. [Fig. 10] shows the result of the function call. [Fig. 10] (Order 18) Students who are within the sets of {Grade 1, Grade 2, Grade 3} and {male, female} are students in the box colored yellow as in [Fig. 11]. Among them, students whose scores are greater than or equal to 1000 are colored blue. Among the students with blue font color, return 500 for the ID of the student with the lowest score. [Fig. 11] [Constraints] 1. init() is called in the beginning of each test case. 2. For each test case, the total sum of add() function call is less than or equal to 20,000. 3. For each test case, the sum of calls of the all functions is less than or equal to 80,000. [Input and Output] As the input and output are processed in the provided code in the Main, they are not processed separately in the User Code. The output result for the sample input is in the format of “#TC number result.” It is the correct answer if the result is 100; it is the wrong answer if it is 0. ------------------------------------------------------------------------------------------------------ package h220; import java.util.HashMap; import java.util.TreeSet; public class UserSolution { class Student{ int id,grade,gender,score; public Student(int id, int grade, int gender, int score) { this.id = id; this.grade = grade; this.gender = gender; this.score = score; } } HashMap<Integer, Student> save; TreeSet<Student> map[][]; void init(){ save = new HashMap<Integer, UserSolution.Student>(); map = new TreeSet[4][3]; for(int i =0;i<4;i++){ for(int j=0;j<3;j++){ map[i][j] = new TreeSet<UserSolution.Student>((a,b) -> a.score == b.score ? a.id - b.id : a.score - b.score); } } } int add(int mId, int mGrade, char mGender[], int mScore){ int gender = (new String(mGender)).trim().equals("male") ? 0 : 1; Student student = new Student(mId, mGrade, gender, mScore); save.put(mId, student); TreeSet<Student> set = map[mGrade][gender]; set.add(student); return set.last().id; } int remove(int mId){ Student student = save.get(mId); if(student == null) return 0; save.remove(mId); TreeSet<Student> set = map[student.grade][student.gender]; set.remove(student); if(set.size() ==0) return 0; return set.first().id; } int query(int mGradeCnt, int mGrade[], int mGenderCnt, char mGender[][], int mScore){ int id = Integer.MAX_VALUE; int score = Integer.MAX_VALUE; for(int i=0; i<mGradeCnt; i++){ for(int j =0; j< mGenderCnt; j++){ int gender = (new String(mGender[j])).trim().equals("male") ? 0 : 1; TreeSet<Student> set = map[mGrade[i]][gender]; Student student = set.ceiling(new Student(0, 0, 0, mScore)); if(student != null){ if(score > student.score || (score == student.score && id > student.id)){ score = student.score; id = student.id; } } } } if(id == Integer.MAX_VALUE) return 0; return id; } }

Leave a Comment