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 Input
5 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
999999
Sample Output
1 C
1 M
1 E
1 A
3 A
N/A
这是一道好长的题目,但是题目的逻辑其实不难。主要是考察结构体的排序问题。
像这种很明显的每个节点是一个结构体类型。
根据priority的优先级,先对每位学生的Average成绩进行排名,再对C,对M,对E进行排名,每次判断一下上一次排名和当前排名名次的前后,只有名次上升了才去更新排名。
代码如下:
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
const int MAX = 2001;
struct Student {
string id;
double C_score;
double M_score;
double E_score;
double A_score;
};
struct OrderedStudent {
string id;
string subject;
int rank;
};
// 用于sort的方式
bool cmpWithC(Student s1, Student s2) {
return s1.C_score > s2.C_score;
}
bool cmpWithM(Student s1, Student s2) {
return s1.M_score > s2.M_score;
}
bool cmpWithE(Student s1, Student s2) {
return s1.E_score > s2.E_score;
}
bool cmpWithA(Student s1, Student s2) {
return s1.A_score > s2.A_score;
}
int main(int argc, const char * argv[]) {
// 获取输入的数据
int N, M;
cin >> N >>M;
// 每个学生的成绩情况
Student records[MAX];
for (int i = 0; i < N; ++i) {
string id;
double C, M, E, A;
cin >> id >> C >> M >> E;
A = (C + M + E) / 3;
Student student;
student.id = id;
student.C_score = C;
student.M_score = M;
student.E_score = E;
student.A_score = A;
records[i] = student;
}
// 想要查看排名的学生
string checkers[MAX];
for (int i = 0; i < M; ++i) {
cin >> checkers[i];
}
//分别进行排序
map<string, OrderedStudent> result;
//对average进行排序
sort(records, records + N, cmpWithA);
for (int i = 0; i < N; ++i) {
string id = records[i].id;
OrderedStudent orderedStu;
orderedStu.subject = "A";
if (i != 0) {
//对于两个成绩相同的人,排一样的名次
if (records[i].A_score == records[i - 1].A_score) {
orderedStu.rank = i;
} else {
orderedStu.rank = i + 1;
}
} else {
orderedStu.rank = i + 1;
}
result[id] = orderedStu;
}
//对C进行排序
sort(records, records + N, cmpWithC);
for (int i = 0; i < N; ++i) {
string id = records[i].id;
int rank;
if (i != 0) {
if (records[i].C_score == records[i - 1].C_score) {
rank = i;
} else {
rank = i + 1;
}
} else {
rank = i + 1;
}
if (rank < result[id].rank) {
result[id].rank = rank;
result[id].subject = "C";
}
}
//对M进行排序
sort(records, records + N, cmpWithM);
for (int i = 0; i < N; ++i) {
string id = records[i].id;
int rank;
if (i != 0) {
if (records[i].M_score == records[i - 1].M_score) {
rank = i;
} else {
rank = i + 1;
}
} else {
rank = i + 1;
}
if (rank < result[id].rank) {
result[id].rank = rank;
result[id].subject = "M";
}
}
//对E进行排序
sort(records, records + N, cmpWithE);
for (int i = 0; i < N; ++i) {
string id = records[i].id;
int rank;
if (i != 0) {
if (records[i].E_score == records[i - 1].E_score) {
rank = i;
} else {
rank = i + 1;
}
} else {
rank = i + 1;
}
if (rank < result[id].rank) {
result[id].rank = rank;
result[id].subject = "E";
}
}
// checkers来搜索了
map<string, OrderedStudent>::iterator it;
for (int i = 0; i < M; ++i) {
string id = checkers[i];
it = result.find(id);
if (it != result.end()) {
cout << (*it).second.rank << " " << (*it).second.subject << endl;
}else {
cout << "N/A" << endl;
}
}
return 0;
}