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 A N/A
思路分析:题意要找出某个学生最佳排名的课程,和相应的课程排名。排名规则:A>C>M>E,其中A是平均分。需要注意排名有并列情况,例如样例给出的id为1和2的英语成绩都是88,所以他们的排名也都是相同的。要不然AC不了。
#include <cstdio>
#include <algorithm>
#include <string.h>
#define MAX 3000
using namespace std;
typedef struct {
char id[7];
int C;
int M;
int E;
double ave;
int rank[4];
int bestRank;
char bestCourse;
} Stu;
Stu stu[MAX];
int cmpAve( Stu a, Stu b ) {
return a.ave > b.ave ? 1 : 0;
}
int cmpC( Stu a, Stu b ) {
return a.C > b.C ? 1 : 0;
}
int cmpM( Stu a, Stu b ) {
return a.M > b.M ? 1 : 0;
}
int cmpE( Stu a, Stu b ) {
return a.E > b.E ? 1 : 0;
}
int main() {
//freopen( "123.txt", "r", stdin );
int n, m;
scanf( "%d%d", &n, &m );
for( int i = 0; i < n; i++ ) {
scanf( "%s %d %d %d", stu[i].id, &stu[i].C, &stu[i].M, &stu[i].E );
stu[i].ave = ( stu[i].C + stu[i].M + stu[i].E ) / 3.0;
}
// 按平均成绩排序
sort( stu, stu + n, cmpAve );
int curRank = 1;
double preAve = 0.0;
for( int i = 0; i < n; i++ ) {
if( i == 0 ) {
preAve = stu[i].ave;
}
else if( stu[i].ave < preAve ) {
curRank = i + 1;
preAve = stu[i].ave;
}
stu[i].rank[0] = curRank;
}
// 按C成绩排序
sort( stu, stu + n, cmpC );
curRank = 1;
int preScore = 0;
for( int i = 0; i < n; i++ ) {
if( i == 0 ) {
preScore = stu[i].C;
}
else if( stu[i].C < preScore ) {
curRank = i + 1;
preScore = stu[i].C;
}
stu[i].rank[1] = curRank;
}
// 按M成绩排序
sort( stu, stu + n, cmpM );
curRank = 1;
preScore = 0;
for( int i = 0; i < n; i++ ) {
if( i == 0 ) {
preScore = stu[i].M;
}
else if( stu[i].M < preScore ) {
curRank = i + 1;
preScore = stu[i].M;
}
stu[i].rank[2] = curRank;
}
// 按E成绩排序
sort( stu, stu + n, cmpE );
curRank = 1;
preScore = 0;
for( int i = 0; i < n; i++ ) {
if( i == 0 ) {
preScore = stu[i].E;
}
else if( stu[i].E < preScore ) {
curRank = i + 1;
preScore = stu[i].E;
}
stu[i].rank[3] = curRank;
}
// 查找最佳课程的排名
for( int i = 0; i < n; i++ ) {
int best = stu[i].rank[0];
int course = 0;
for( int j = 0; j < 4; j++ ) {
if( stu[i].rank[j] < best ) {
best = stu[i].rank[j];
course = j;
}
}
stu[i].bestRank = best;
if( course == 0 ) stu[i].bestCourse = 'A';
else if( course == 1 ) stu[i].bestCourse = 'C';
else if( course == 2 ) stu[i].bestCourse = 'M';
else if( course == 3 ) stu[i].bestCourse = 'E';
}
/*
for( int i = 0; i < n; i++ ) {
printf( "%s ", stu[i].id );
for( int j = 0; j < 4; j++ ) {
printf( "%d ", stu[i].rank[j] );
}
printf( "%d %c", stu[i].bestRank, stu[i].bestCourse );
printf( "\n" );
}
*/
char queryID[7];
for( int i = 0; i < m; i++ ) {
bool flag = false;
scanf( "%s", queryID );
for( int j = 0; j < n; j++ ) {
if( !strcmp( stu[j].id, queryID ) ) {
flag = true;
printf( "%d %c\n", stu[j].bestRank, stu[j].bestCourse );
break;
}
}
if( !flag ) {
printf( "N/A\n" );
}
}
return 0;
}