PAT 1012. The Best Rank (25)
1012. The Best Rank (25)
To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algebra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks -- that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.
For example, The grades of C, M, E and A - Average of 4 students are given as the following:
StudentID C M E A 310101 98 85 88 90 310102 70 95 88 84 310103 82 87 94 88 310104 91 91 91 91
Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.
Input
Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (<=2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.
Output
For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.
The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.
If a student is not on the grading list, simply output "N/A".
Sample Input5 6 310101 98 85 88 310102 70 95 88 310103 82 87 94 310104 91 91 91 310105 85 90 90 310101 310102 310103 310104 310105 999999Sample Output
1 C 1 M 1 E 1 A 3 A N/A
注意同分数的排名相同
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <algorithm> 5 6 using namespace std; 7 8 class Score 9 { 10 private: 11 string id; 12 int C, M, E, A; 13 int CRank, MRank, ERank, ARank; 14 int BestRank; 15 char BestCourse; 16 public: 17 friend bool Ccmp(const Score &s1, const Score &s2); 18 friend bool Mcmp(const Score &s1, const Score &s2); 19 friend bool Ecmp(const Score &s1, const Score &s2); 20 friend bool Acmp(const Score &s1, const Score &s2); 21 friend bool Idcmp(const Score &s1, const Score &s2); 22 friend istream& operator>>(istream &is, Score &s); 23 void SetCRank(int Rank){ CRank = Rank; } 24 void SetMRank(int Rank){ MRank = Rank; } 25 void SetERank(int Rank){ ERank = Rank; } 26 void SetARank(int Rank){ ARank = Rank; } 27 void SetBest(); 28 string GetId(){ return id; } 29 int GetC(){ return C; } 30 int GetM(){ return M; } 31 int GetE(){ return E; } 32 int GetA(){ return A; } 33 int GetBestRank(){ return BestRank; } 34 char GetBestCourse(){ return BestCourse; } 35 }; 36 37 bool Ccmp(const Score &s1, const Score &s2) 38 { 39 return s1.C > s2.C; 40 } 41 bool Mcmp(const Score &s1, const Score &s2) 42 { 43 return s1.M > s2.M; 44 } 45 bool Ecmp(const Score &s1, const Score &s2) 46 { 47 return s1.E > s2.E; 48 } 49 bool Acmp(const Score &s1, const Score &s2) 50 { 51 return s1.A > s2.A; 52 } 53 bool Idcmp(const Score &s1, const Score &s2) 54 { 55 return s1.id < s2.id; 56 } 57 58 istream& operator>>(istream &is, Score &s) 59 { 60 is >> s.id >> s.C >> s.M >> s.E; 61 s.A = (s.C + s.M + s.E) / 3; 62 63 return is; 64 } 65 66 void Score::SetBest() 67 { 68 if (ARank <= CRank&&ARank <= MRank&&ARank <= ERank) 69 { 70 BestRank = ARank; 71 BestCourse = 'A'; 72 } 73 else if (CRank <= MRank&&CRank <= ERank) 74 { 75 BestRank = CRank; 76 BestCourse = 'C'; 77 } 78 else if (MRank <= ERank) 79 { 80 BestRank = MRank; 81 BestCourse = 'M'; 82 } 83 else 84 { 85 BestRank = ERank; 86 BestCourse = 'E'; 87 } 88 } 89 90 int find(string &id, vector<Score> &Scores, int left, int right) 91 { 92 int middle = (left + right) / 2; 93 94 if (left == right&&Scores[middle].GetId() != id) 95 return -1; 96 else if (Scores[middle].GetId() == id) 97 return middle; 98 else if (Scores[middle].GetId() > id) 99 return find(id, Scores, left, middle); 100 else 101 return find(id, Scores, middle+1, right); 102 } 103 104 int main() 105 { 106 vector<Score> Scores; 107 Score tmp; 108 int ScoreNum, QueryNum; 109 110 cin >> ScoreNum >> QueryNum; 111 while (ScoreNum--) 112 { 113 cin >> tmp; 114 Scores.push_back(tmp); 115 } 116 int lastScore, lastRank; 117 //set C-programming Rank 118 lastScore = lastRank = -1; 119 sort(Scores.begin(), Scores.end(), Ccmp); 120 for (int i = 0; i < Scores.size(); i++) 121 { 122 if (Scores[i].GetC() == lastScore) 123 Scores[i].SetCRank(lastRank); 124 else 125 { 126 Scores[i].SetCRank(i + 1); 127 lastScore = Scores[i].GetC(); 128 lastRank = i + 1; 129 } 130 } 131 //set Mathematics rank 132 lastScore = lastRank = -1; 133 sort(Scores.begin(), Scores.end(), Mcmp); 134 for (int i = 0; i < Scores.size(); i++) 135 { 136 if (Scores[i].GetM() == lastScore) 137 Scores[i].SetMRank(lastRank); 138 else 139 { 140 Scores[i].SetMRank(i + 1); 141 lastScore = Scores[i].GetM(); 142 lastRank = i + 1; 143 } 144 } 145 //set English Rank 146 lastScore = lastRank = -1; 147 sort(Scores.begin(), Scores.end(), Ecmp); 148 for (int i = 0; i < Scores.size(); i++) 149 { 150 if (Scores[i].GetE() == lastScore) 151 Scores[i].SetERank(lastRank); 152 else 153 { 154 Scores[i].SetERank(i + 1); 155 lastScore = Scores[i].GetE(); 156 lastRank = i + 1; 157 } 158 } 159 //set average rank 160 lastScore = lastRank = -1; 161 sort(Scores.begin(), Scores.end(), Acmp); 162 for (int i = 0; i < Scores.size(); i++) 163 { 164 if (Scores[i].GetA() == lastScore) 165 Scores[i].SetARank(lastRank); 166 else 167 { 168 Scores[i].SetARank(i + 1); 169 lastScore = Scores[i].GetA(); 170 lastRank = i + 1; 171 } 172 } 173 for (int i = 0; i < Scores.size(); i++) 174 Scores[i].SetBest(); 175 sort(Scores.begin(), Scores.end(), Idcmp); 176 177 for (int i = 0; i < QueryNum; i++) 178 { 179 string id; 180 cin >> id; 181 int index = find(id, Scores, 0, Scores.size()-1); 182 if (index == -1) 183 cout << "N/A"; 184 else 185 cout << Scores[index].GetBestRank() << " " << Scores[index].GetBestCourse(); 186 if (i != QueryNum - 1) 187 cout << endl; 188 } 189 }