问题
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 Algrbra), 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 Specification:
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 Specification:
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
思路
结构体存储所有学生各科成绩、学号、各科排名。排名采用map<int,char>存储,由于map默认按照key从小到大排序,所以输出该学生最好排名的时候直接输出map中第一个元素即可。
代码
#include <bits/stdc++.h>
using namespace std;
struct student{
int id;
int C;
int M;
int E;
int A;
map<int,char> max;
student(){}
student(int _id,int _C,int _M,int _E,int _A):id(_id),C(_C),M(_M),E(_E),A(_A){}
};
bool cmpC(student a,student b){
return a.C>b.C;
};
bool cmpM(student a,student b){
return a.M>b.M;
};
bool cmpE(student a,student b){
return a.E>b.E;
};
bool cmpA(student a,student b){
return a.A>b.A;
};
vector<student>ranks;
int main(){
int n,m,C,M,E,A;
cin>>n>>m;
int id,temp;
for (int i = 0; i < n; ++i) {
cin>>id>>C>>M>>E;
A = round((C+M+E)/3.0);
student s(id,C,M,E,A);
ranks.push_back(s);
}
sort(ranks.begin(),ranks.end(),cmpE);//如果分数相同,排名也相同
for (int k = 0; k < ranks.size(); ++k) {
if(k==0){
ranks[k].max[k+1] = 'E';
continue;
}
if(k>0 && ranks[k].E ==ranks[k-1].E){
map<int,char> ::iterator it = ranks[k-1].max.begin();
for (;it!= ranks[k-1].max.end() ; it++) {
if(it->second=='E'){
temp = it->first;
break;
}
}
ranks[k].max[temp] = 'E';
} else{
ranks[k].max[k+1] = 'E';
}
}
sort(ranks.begin(),ranks.end(),cmpM);
for (int k = 0; k < ranks.size(); ++k) {
if(k==0){
ranks[k].max[k+1] = 'M';
continue;
}
if(k>0 && ranks[k].M ==ranks[k-1].M){
map<int,char> ::iterator it = ranks[k-1].max.begin();
for (;it!= ranks[k-1].max.end() ; it++) {
if(it->second=='M'){
temp = it->first;
break;
}
}
ranks[k].max[temp] = 'M';
} else{
ranks[k].max[k+1] = 'M';
}
}
sort(ranks.begin(),ranks.end(),cmpC);
for (int k = 0; k < ranks.size(); ++k) {
if(k==0){
ranks[k].max[k+1] = 'C';
continue;
}
if(k>0 && ranks[k].C ==ranks[k-1].C){
map<int,char> ::iterator it = ranks[k-1].max.begin();
for (;it!= ranks[k-1].max.end() ; it++) {
if(it->second=='C'){
temp = it->first;
break;
}
}
ranks[k].max[temp] = 'C';
} else{
ranks[k].max[k+1] = 'C';
}
}
sort(ranks.begin(),ranks.end(),cmpA);
for (int k = 0; k < ranks.size(); ++k) {
if(k==0){
ranks[k].max[k+1] = 'A';
continue;
}
if(k>0 && ranks[k].A ==ranks[k-1].A){
map<int,char> ::iterator it = ranks[k-1].max.begin();
for (;it!= ranks[k-1].max.end() ; it++) {
if(it->second=='A'){
temp = it->first;
break;
}
}
ranks[k].max[temp] = 'A';
} else{
ranks[k].max[k+1] = 'A';
}
}
for (int j = 0; j < m; ++j) {
bool flag=false;
cin>>id;
for (int i = 0; i < ranks.size(); ++i) {
if(ranks[i].id==id){
map<int,char>::iterator it = ranks[i].max.begin();
cout<<it->first<<" "<<it->second<<endl;
flag = true;
break;
}
}
if(!flag)
cout<<"N/A"<<endl;
}
return 0;
}
总结
本题最应该注意的两点:
1.如果一个学生的多门成绩有相同的排名,则按照题目给定的优先级,输出优先级高的一门。所以更新map的值的时候要注意顺序,应该把优先级高的课程放在后面,这样如果覆盖了map,最后保存下来的最高排名一定是优先级较大的那一门课。
2.同一门课程中,相同分数拥有相同的排名。例如C语言课程的成绩 92 88 88 88 80。排名就是1 2 2 2 5