1012. The Best Rank (25)

来源:互联网 发布:安卓7.0 java模拟器 编辑:程序博客网 时间:2024/06/13 23:51

这道题其实就是排序+查找,但是排序是结构体排序,查找不知道直接O(N)查找会不会超时,我是用的map。

比较坑的一点是多个重分排名时,其名次应该相同,更重要的一点是,之后的人的名次不是上一个名次+1,而是+重复的人数!!如:1 2 2 4而不是1 2 2 3.


1 C1 M1 E1 A3 AN/A
1 C1 M1 E1 A3 AN/A

o 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  A310101     98 85 88 90310102     70 95 88 84310103     82 87 94 88310104     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 Input
5 6310101 98 85 88310102 70 95 88310103 82 87 94310104 91 91 91310105 85 90 90310101310102310103310104310105999999
Sample Output
1 C1 M1 E1 A3 AN/A
// 1012. The Best Rank.cpp : Defines the entry point for the console application.//#include<iostream>#include<vector>#include<string>#include<map>#include<algorithm>using namespace std;typedef struct {char courseName;//A, C, M, Edouble grades;//A, C, M, Eint rank;//A, C, M, E}StudInfo;typedef pair<string, StudInfo> PAIR;typedef map<string, StudInfo> STUDMAP;int Compare( const PAIR lhs, const PAIR rhs){return lhs.second.grades > rhs.second.grades;}void SortMap(STUDMAP* gradeMap){int rankA = 0, rankC = 0, rankM = 0, rankE = 0;int accumTimeA = 1, accumTimeC = 1, accumTimeM = 1, accumTimeE = 1 ;STUDMAP &gradeMap4A = gradeMap[0], &gradeMap4C = gradeMap[1],&gradeMap4M = gradeMap[2], &gradeMap4E = gradeMap[3];vector<PAIR>gradeVec4A(gradeMap4A.begin(), gradeMap4A.end());vector<PAIR>gradeVec4C(gradeMap4C.begin(), gradeMap4C.end());vector<PAIR>gradeVec4M(gradeMap4M.begin(), gradeMap4M.end());vector<PAIR>gradeVec4E(gradeMap4E.begin(), gradeMap4E.end());sort(gradeVec4A.begin(), gradeVec4A.end(), Compare);sort(gradeVec4C.begin(), gradeVec4C.end(), Compare);sort(gradeVec4M.begin(), gradeVec4M.end(), Compare);sort(gradeVec4E.begin(), gradeVec4E.end(), Compare);for (int i = 0; i < gradeMap4A.size(); i++){if (i == 0 || gradeVec4A[i].second.grades < gradeVec4A[i - 1].second.grades){rankA += accumTimeA;accumTimeA = 1;}elseaccumTimeA++;gradeMap4A.find(gradeVec4A[i].first)->second.rank = rankA;if (i == 0 || gradeVec4C[i].second.grades < gradeVec4C[i - 1].second.grades){rankC += accumTimeC;accumTimeC = 1;}elseaccumTimeC++;gradeMap4C.find(gradeVec4C[i].first)->second.rank = rankC;if (i == 0 || gradeVec4M[i].second.grades < gradeVec4M[i - 1].second.grades){rankM += accumTimeM;accumTimeM = 1;}elseaccumTimeM++;gradeMap4M.find(gradeVec4M[i].first)->second.rank = rankM;if (i == 0 || gradeVec4E[i].second.grades < gradeVec4E[i - 1].second.grades){rankE += accumTimeE;accumTimeE = 1;}elseaccumTimeE++;gradeMap4E.find(gradeVec4E[i].first)->second.rank = rankE;}return;}pair<int, char> Query(string ID, STUDMAP* gradeMap){STUDMAP &gradeMap4A = gradeMap[0], &gradeMap4C = gradeMap[1],&gradeMap4M = gradeMap[2], &gradeMap4E = gradeMap[3];char tag[4] = { 'E', 'M', 'C', 'A' };//4:A, 3:C, 2:M, 1:Epair<int, char> rankList[4], bestRank = pair<int, char>(gradeMap4A.size(), 0);map<string, StudInfo>::iterator iter;iter = gradeMap4A.find(ID);if (iter == gradeMap4A.end()){return pair<int, char>(0, 0);}rankList[0] = pair<int, char>(iter->second.rank, 4);iter = gradeMap4C.find(ID);rankList[1] = pair<int, char>(iter->second.rank, 3);iter = gradeMap4M.find(ID);rankList[2] = pair<int, char>(iter->second.rank, 2);iter = gradeMap4E.find(ID);rankList[3] = pair<int, char>(iter->second.rank, 1);for (int i = 0; i < 4; i++){if (rankList[i].first < bestRank.first ||(rankList[i].first == bestRank.first&&rankList[i].second > bestRank.second))bestRank = rankList[i];}bestRank.second = tag[bestRank.second - 1];return bestRank;}int main(){STUDMAP* gradeMap = new STUDMAP[4];//封装传参STUDMAP &gradeMap4A = gradeMap[0], &gradeMap4C = gradeMap[1], &gradeMap4M = gradeMap[2], &gradeMap4E = gradeMap[3];int N, M;string ID;double grade4A, grade4C, grade4M, grade4E;cin >> N >> M;while (N--){cin >> ID >> grade4C >> grade4M >> grade4E;grade4A = (grade4C + grade4M + grade4E) / 3;gradeMap4A.insert(map<string, StudInfo>::value_type(ID, StudInfo{ 'A', grade4A, -1 }));gradeMap4C.insert(PAIR(ID, StudInfo{ 'C', grade4C, -1 }));gradeMap4M.insert(make_pair(ID, StudInfo{ 'M', grade4M, -1 }));gradeMap4E.insert(map<string, StudInfo>::value_type(ID, StudInfo{ 'E', grade4E, -1 }));}SortMap(gradeMap);while (M--){cin >> ID;pair<int, char> bestRank = Query(ID, gradeMap);if (bestRank.second != 0)cout << bestRank.first << " " << bestRank.second << endl;elsecout << "N/A" << endl;}return 0;}


1 C1 M1 E1 A3 AN/A
1 C1 M1 E1 A3 AN/A
0 0
原创粉丝点击