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
解题
给定学号,各科成绩,算出每个人最好一科的排名;
算排名: 排序之后遍历计算排名;
算每一科的排名: 对每一科都要排序和遍历一次;
查找ID,输出排名: 类似哈希表,也可用二维数组存放【ID】【科目排名】,做快速的查找;
1.学生的结构函数
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXID 1000000
struct stu{
int ID;
int C,M,E,A;
};
2.读入每个学生的ID和成绩,并计算average
vector<stu> S;
//读入后面的人成绩时,更新前面人的排名
int N,M;
void input()
{
cin>>N>>M;
S.resize(N);
for(int i=0;i<N;i++)
{
cin>>S[i].ID>>S[i].C>>S[i].M>>S[i].E;
S[i].A=(S[i].C+S[i].M+S[i].E)/3;
}
}
3.定义二维数组RANK,计算学生每个项目的排名并放入RANK中;
bool compareA(stu a,stu b){return a.A>b.A;}
bool compareC(stu a,stu b){return a.C>b.C;}
bool compareM(stu a,stu b){return a.M>b.M;}
bool compareE(stu a,stu b){return a.E>b.E;}
int RANK[MAXID][4]; //代表四个成绩每个的排名
int Rank()
{
memset(RANK,0,sizeof RANK); //
int cnt=1;
//先根据A排序
sort(S.begin(),S.end(),compareA);
for(int i=0;i<S.size();i++)
{
if(i>0&&S[i].A==S[i-1].A)
RANK[S[i].ID][0]=RANK[S[i-1].ID][0];
else
RANK[S[i].ID][0]=cnt;
cnt++;
}
cnt=1;
sort(S.begin(),S.end(),compareC);
for(int i=0;i<S.size();i++)
{
if(i>0&&S[i].C==S[i-1].C)
RANK[S[i].ID][1]=RANK[S[i-1].ID][1];
else
RANK[S[i].ID][1]=cnt;
cnt++;
}
cnt=1;
sort(S.begin(),S.end(),compareM);
for(int i=0;i<S.size();i++)
{
if(i>0&&S[i].M==S[i-1].M)
RANK[S[i].ID][2]=RANK[S[i-1].ID][2];
else
RANK[S[i].ID][2]=cnt;
cnt++;
}
cnt=1;
sort(S.begin(),S.end(),compareE);
for(int i=0;i<S.size();i++)
{
if(i>0&&S[i].E==S[i-1].E)
RANK[S[i].ID][3]=RANK[S[i-1].ID][3];
else
RANK[S[i].ID][3]=cnt;
cnt++;
}
}
4.查找,读入查找的ID,搜索其最高成绩;
void Search()
{
//将S映射在哈希表上
int ID;
int index;
int MR;
for(int i=0;i<M;i++)
{
cin>>ID;
MR=0x3f3f3f;
for(int j=0;j<4;j++){
if(RANK[ID][j]<MR){
MR=RANK[ID][j];
index = j;
}
}
if(MR>0)cout<<MR<<" "<<Courses[index]<<endl;
else cout<<"N/A"<<endl;
}
}
5.main函数
int main()
{
input();
Rank();
Search();
}
总结
空间占用较大,耗时较长;
可优化查找过程,先计算出最高rank直接输出;
可用map储存代替二维数组,减少空间占用;