题目描述:
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.
翻译:为了评价我们的首届CS专业学生的表现,我们仅从C—C语言编程,M—数学(积分或线性代数)和英语。与此同时,我们鼓励学生去突出他们最好的排名——这代表着,在这三门课程和平均等级中,我们输出每位学生的最高评级。
举个例子,每位学生的C,M,E,A成绩如下:
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
所有这些学生的最高评级都是第一名,第一个是C语言,第二个是数学,第三是英语,最后一个是平均分。
INPUT FORMAT
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.
翻译:每个输入文件包括一组测试数据。每组测试数据第一行为两个数字N和M (<=2000),分别为学生的总数和想要查询自己评级的学生。接下来的N行,每行包括一个6位学生ID,接下来是该生的C,M,E成绩。之后的M行每行都是一个学生ID。
OUTPUT FORMAT
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”.
翻译:对于M个学生,每个都在一行内输出他/她的最高评级和对应的科目,用空格隔开。评价的优先级为A>C>M>E。因此如果有2个及以上方法去得到同样的评级,输出最高优先级的那个。
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保存学生对应位置,然后对4种成绩排序。按照A > C > M > E的顺序查询最高评级,注意如果两个分数相同,他们都是同一名,例如:98,98,97,为1、1、3。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
#define INF 99999999
using namespace std;
typedef pair<int,int> P;
map<string,int> mp;
vector<P>C,M,E,A;
int N,K,ccount=0;
int ans[2010][2];
bool cmp(P a,P b){
return a.first>b.first;
}
int main(){
scanf("%d%d",&N,&K);
string s;
int a,b,c;
for(int i=0;i<N;i++){
cin>>s;
scanf("%d%d%d",&a,&b,&c);
C.push_back(P(a,ccount+1));
M.push_back(P(b,ccount+1));
E.push_back(P(c,ccount+1));
A.push_back(P((a+b+c)/3,ccount+1));
mp[s]=ccount+1;
ccount++;
}
sort(C.begin(),C.end(),cmp);
sort(M.begin(),M.end(),cmp);
sort(E.begin(),E.end(),cmp);
sort(A.begin(),A.end(),cmp);
for(int i=0;i<=N;i++)ans[i][1]=INF;
for(int i=0;i<N;i++){
int temp=i;
while(temp>0&&A[temp].first==A[temp-1].first)temp--;
if(temp<ans[A[i].second][1])ans[A[i].second][1]=temp,ans[A[i].second][0]=0;
}
for(int i=0;i<N;i++){
int temp=i;
while(temp>0&&C[temp].first==C[temp-1].first)temp--;
if(temp<ans[C[i].second][1])ans[C[i].second][1]=temp,ans[C[i].second][0]=2;
}
for(int i=0;i<N;i++){
int temp=i;
while(temp>0&&M[temp].first==M[temp-1].first)temp--;
if(temp<ans[M[i].second][1])ans[M[i].second][1]=temp,ans[M[i].second][0]=12;
}
for(int i=0;i<N;i++){
int temp=i;
while(temp>0&&E[temp].first==E[temp-1].first)temp--;
if(temp<ans[E[i].second][1])ans[E[i].second][1]=temp,ans[E[i].second][0]=4;
}
for(int i=0;i<K;i++){
cin>>s;
if(!mp[s])printf("N/A\n");
else printf("%d %c\n",ans[mp[s]][1]+1,ans[mp[s]][0]+'A');
}
return 0;
}