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), andE
- 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
andA
- 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.
解题思路:有可能存在同分情况,下标为0肯定是第一名,如果与前面分数不一样那就排名加一
输入
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
andE
. Then there are M lines, each containing a student ID.
输出
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
.
#include<iostream>
#include<unordered_map>
#include<algorithm>
using namespace std;
struct student{
string ID;
int grade[4],rank[4];
};
bool comp0(student x,student y){
return x.grade[0]>y.grade[0];
}
bool comp1(student x,student y){
return x.grade[1]>y.grade[1];
}
bool comp2(student x,student y){
return x.grade[2]>y.grade[2];
}
bool comp3(student x,student y){
return x.grade[3]>y.grade[3];
}
int main(){
student s[2000];
int N,M,i,j,k,l;
cin>>N>>M;
for(i=0;i<N;i++){
string temp;
cin>>temp>>j>>k>>l;
s[i].ID=temp;
s[i].grade[0]=(j+k+l)/3;
s[i].grade[1]=j;
s[i].grade[2]=k;
s[i].grade[3]=l;
}
sort(s,s+N,comp0);
for(i=0;i<N;i++){
if(i==0||s[i].grade[0]!=s[i-1].grade[0])j=i+1;
s[i].rank[0]=j;
//有可能存在同分情况,下标为0肯定是第一名,如果与前面分数不一样那就排名加一
}//根据C排名
sort(s,s+N,comp1);
for(i=0;i<N;i++){
if(i==0||s[i].grade[1]!=s[i-1].grade[1])j=i+1;
s[i].rank[1]=j;
}//根据M
sort(s,s+N,comp2);
for(i=0;i<N;i++){
if(i==0||s[i].grade[2]!=s[i-1].grade[2])j=i+1;
s[i].rank[2]=j;
}//根据E
sort(s,s+N,comp3);
for(i=0;i<N;i++){
if(i==0||s[i].grade[3]!=s[i-1].grade[3])j=i+1;
s[i].rank[3]=j;
}//根据A
unordered_map<string,int>MAP;
for(i=0;i<N;i++)MAP[s[i].ID]=i;//建立哈希表
while(M--){
//应对M次的查询
string temp;
cin>>temp;
if(!MAP.count(temp)){
//如果没有temp
cout<<"N/A"<<endl;
}
else{
//如果有temp
i=MAP[temp];
int minrank=10000;
//找到哪一科最好,排名是多少
for(j=0;j<4;j++){
if(minrank>s[i].rank[j])minrank=s[i].rank[j];
}
if(minrank==s[i].rank[0])cout<<minrank<<' '<<'A'<<endl;
else if(minrank==s[i].rank[1])cout<<minrank<<' '<<'C'<<endl;
else if(minrank==s[i].rank[2])cout<<minrank<<' '<<'M'<<endl;
else if(minrank==s[i].rank[3])cout<<minrank<<' '<<'E'<<endl;
}
}
}