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 AN/A
还剩考sort函数的题,注意cmp函数的定义即可
#include <iostream> #include <stdio.h> #include <algorithm> #include <string> #include <map> #include <iterator> using namespace std; struct Stu{ string id; int c,m,e,a; int bestrank,bestcourse; }stu[2011]; bool cmpc(Stu s1,Stu s2){ if(s1.c>s2.c) return true; return false; } bool cmpm(Stu s1,Stu s2){ if(s1.m>s2.m) return true; return false; } bool cmpe(Stu s1,Stu s2){ if(s1.e>s2.e) return true; return false; } bool cmpa(Stu s1,Stu s2){ if(s1.a>s2.a) return true; return false; } int main(){ map<string,int> idtoindex; //freopen("E:\input.txt","r",stdin); int N,M; cin>>N>>M; for(int i=0;i<N;i++){ cin>>stu[i].id>>stu[i].c>>stu[i].m>>stu[i].e; stu[i].a=(stu[i].c+stu[i].m+stu[i].e)/3; } //a sort(stu,stu+N,cmpa); int temprank[2000]; memset(temprank,1,0); stu[0].bestrank=1; stu[0].bestcourse=0; for(int i=1;i<N;i++){ if(stu[i].a==stu[i-1].a) stu[i].bestrank=stu[i-1].bestrank; else stu[i].bestrank=i+1; stu[0].bestcourse=0; } //c sort(stu,stu+N,cmpc); temprank[0]=1; int i=0; if(temprank[i]<stu[i].bestrank){ stu[i].bestrank=temprank[i]; stu[i].bestcourse=1; i++;} for(;i<N;i++){ if(stu[i].c==stu[i-1].c) temprank[i]=temprank[i-1]; else temprank[i]=i+1; if(temprank[i]<stu[i].bestrank){ stu[i].bestrank=temprank[i]; stu[i].bestcourse=1; } } //m sort(stu,stu+N,cmpm); temprank[0]=1; i=0; if(temprank[i]<stu[i].bestrank){ stu[i].bestrank=temprank[i]; stu[i].bestcourse=2; i++;} for(;i<N;i++){ if(stu[i].m==stu[i-1].m) temprank[i]=temprank[i-1]; else temprank[i]=i+1; if(temprank[i]<stu[i].bestrank){ stu[i].bestrank=temprank[i]; stu[i].bestcourse=2; } } //e sort(stu,stu+N,cmpe); temprank[0]=1; i=0; idtoindex[stu[i].id]=i; if(temprank[i]<stu[i].bestrank){ stu[i].bestrank=temprank[i]; stu[i].bestcourse=3; i++;} for(;i<N;i++){ idtoindex[stu[i].id]=i; if(stu[i].e==stu[i-1].e) temprank[i]=temprank[i-1]; else temprank[i]=i+1; if(temprank[i]<stu[i].bestrank){ stu[i].bestrank=temprank[i]; stu[i].bestcourse=3; } } string str; char course[5]={"ACME"}; for(int j=0;j<M;j++){ cin>>str; map<string,int>::iterator iter; if((iter=idtoindex.find(str))==idtoindex.end()) cout<<"N/A\n"; else{ int index=idtoindex[str]; cout<<stu[index].bestrank<<" "<<course[stu[index].bestcourse]; if(j!=M-1) cout<<endl; } } return 0; }